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 an 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.
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 an 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.
A switch statement tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier.
in this section we are going to learn three keywords switch, case and default. Apart from these keywords, we will also see the 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
}
…..
}
The switch statement is used to solve multiple option type problems for the menu like a program, where one value is associated with each option. The expression in switch case evaluates to return an integer 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 the menu-driven program. The following example illustrates the menu-driven program. It is also used 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 the 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 the 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 the switch body.
- Whenever wrong selection from menu (other than the 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 the switch body.
Rest you can understand the flow of program by executing it
No comments:
Post a Comment