switch case control instruction
We studied earlier in decision control that how and when to use if, if-else.
They are good enough to select one from the two available options. When choices are two, we can use if-else. Even if we have multiple choices we can use multiple if-else, sometimes we use if-else ladder or nested if-else. You probably experienced that as the number of available options/choices grows, if-else becomes more complex.
Whenever we have many choices(cases) and we have to choose one of them (generally), it is better to use switch case control.
In this section we are going to learn three keywords switch, case and default. Apart from these keywords we will also see usage of break keyword in switch body.
Syntax of switch
int main()
{
….
….
switch( expression)
{
case constant1 :
block-1
case constant2 :
block-2
case constant3 :
block-3
…
default:
default-block
}
…..
}
Switch statement is used to solve multiple option type problems for menu like program, where one value is associated with each option. The expression in switch case evaluates to return an integral value, which is then compared to the values in different cases, where it matches that block of code is executed, if there is no match, then default block is executed.
Switch case control is very useful in menu driven program. The following example illustrates menu driven program. It is also use break to terminate switch.
Example
int main()
{
int a, b, result, ch;
while(1) // condition is always true, thus an infinite loop
{
printf(“\n1. Addition”);
printf(“\n2. Subtraction”);
printf(“\n3. Multiplication”);
printf(“\n4. Division”);
printf(“\n5. Exit”);
printf(“\n\nEnter your choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“Enter two numbers”);
scanf(“%d%d”,&a,&b);
result=a+b;
printf(“Sum is %d”, result);
break; //it is used to move control out of switch body
case 2:
printf(“Enter two numbers”);
scanf(“%d%d”,&a,&b);
result=a-b;
printf(“Difference is %d”, result);
break; //it is used to move control out of switch body
case 3:
printf(“Enter two numbers”);
scanf(“%d%d”,&a,&b);
result=a*b;
printf(“Product is %d”, result);
break; //it is used to move control out of switch body
case 4:
printf(“Enter two numbers”);
scanf(“%d%d”,&a,&b);
result=a/b;
printf(“Quotient is %d”, result);
break; //it is used to move control out of switch body
case 5:
exit(0);
default:
printf(“Invalid Entry”);
}
getch();
}
return(0);
}
Explanation:
There are few things to discuss about above program.
- Notice while loop which executes infinite times till you select 5 from the menu.
- In case 5, we use a predefined function exit. The job of this function is to terminate program. Argument 0 in the function depicts the normal termination. Argument could be 1 passed in the function in the case of abnormal termination.
- The keyword break is used in each case as it transfers the control outside switch body.
- Whenever wrong selection from menu (other than value from 1 to 5) switch moves the control to default segment.
- No need to put keyword break after default statements as it is already at the end of switch body.
Rest you can understand the flow of program by executing it