You are on page 1of 15

Multiway branching statement :

In the case of the multiway branching , we


examine a given expression for the multiple
possible alternatives.In case of "C" language ,
we implement the multi-way branching using
the "switch" statement.
The general form is ,

switch(expression)
{
case value1:
expression 1;
break;
case value2:
expression 2;
break;
case value3:
expression 3;
break;
: : :
: : :
case valuen:
expression n;
break;
default:
expression n+1;
}

Conisder the following code ,

#include<stdio.h>
#include<conio.h>
main()
{
int val;

clrscr();

printf("\nEnter the value of val:");


scanf("%d",&val);

switch(val)
{
case 10:
printf("\nHello");
case 15:
printf("\nBombay");
case 29:
printf("\nJodhpur");
default :
printf("\nJaipur");
}
getch();
}

(a) val=15
output :
Bombay
Jodhpur
Jaipur
If the case holds true , then all the cases below
that case will be considered as true.Therefore ,
we have to make use of the "break" statement ,
which is used to come out of the switch
statement.

(b) val=9

Now, all the above case hold false , so the


statement under the default will get executed.

output :
Jaipur

The above memtioned code will correctly be


written as ,

#include<stdio.h>
#include<conio.h>
main()
{
int val;

clrscr();

printf("\nEnter the value of val:");


scanf("%d",&val);

switch(val)
{
case 10:
printf("\nHello");
break;
case 15:
printf("\nBombay");
break;
case 29:
printf("\nJodhpur");
break;
default :
printf("\nJaipur");
}
getch();
}
val=15
output :
Bombay

Q WAP to read the day number and output the


day name
Day : 1
output :
Monday
#include<stdio.h>
#include<conio.h>
main()
{
int nday;
clrscr();
printf("\nEnter the day number :");
scanf("%d",&nday);

switch(nday)
{
case 1:
printf("\nMonday");
break;
case 2:
printf("\nTuesday");
break;
case 3:
printf("\nWednesday");
break;
case 4:
printf("\nThursday");
break;
case 5:
printf("\nFriday");
break;
case 6:
printf("\nSaturday");
break;
case 7:
printf("\nSunday");
break;
default:
printf("\nInvalid Day
Number.");
}
getch();
}

Q Wap to read the month number and output the


number of days in it.
Month=1
output :
Days = 31

#include<stdio.h>
#include<conio.h>
main()
{
int nmonth;

clrscr();

printf("\nEnter the month number :");


scanf("%d",&nmonth);

switch(nmonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("\nDays = 31.");
break;
case 2:
printf("\nDays = 28");
break;
case 4:
case 6:
case 9:
case 11:
printf("\nDays = 30");
break;
default :
printf("\nInvalid Month Number
");
}
getch();

Q WAP to read two numbers and an operator


and perform the operation specified by the
operator.

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,result,i;
char op;
clrscr();

printf("\nEnter the value of a and b :");


scanf("%d %d",&a,&b);

printf("\nEnter the Operator :");


scanf("%c",&op);

switch(op)
{
case '*':
result = a*b;
break;
case '/':
result =a/b;
break;
case '%':
result=a%b;
break;
case '+':
result = a+b;
break;
case '-':
result=a-b;
break;
case '^':
result=1;

for(i=1;i<=b;i++)
result = result *a;

break;
}

printf("\nResult = %d",result);

getch();
}
;
Q.Find the output the following program ,

(i)
#include<stdio.h>
#include<conio.h>
main()
{
int val=20;

switch(val)
{
case val>=1&&val<=20:
printf("\nCore Java");
break;
case val>=21 && val<=40:
printf("\nAdvanced Java");
break;
default:
printf("\nOracle");
}
getch();
}

output :
Error

Because we cannot use the logical


operators in the case statement. The value in
the front of the case keyword should be a
constant value.

(ii)
#include<stdio.h>
#include<conio.h>
main()
{
float val=12.5;

switch(val)
{
case 10.0:
printf("\n Oracle 9i");
break;
case 12.5:
printf("\n C#");
break;
case 45.0:
printf("\n Visual Basic");
break;
default:
printf("\n Programming in
C");
}
}

output :
Error

Because using the switch statement we


can check only the character and the integer
type expression , we cannot check the
floating point values using the switch statement.

Q. WAP using the switch statement to check


whether the number is even or odd.

#include<stdio.h>
#include<conio.h>
main()
{
int num;

clrscr();

printf("\nEnter the number :");


scanf("%d",&num);

switch(num%2)
{
case 0:
printf("\nEven");
break;
case 1:
printf("\nOdd");
}

getch();
}
==================================
=================================

You might also like