SWITCH STATEMENT
The switch_case statement is the modular replacement of the cumbersome nested if_ else structure.The switch and case statements help to control complex conditional and branching operations.The switch statement transfers control to a statement within its body.The syntax of the switch_case statement is as follows:
switch(conditional expression)
{
case constant-expression 1:
................
break;
case constant-expression 2:
...............
break;
.
default:
...............
}
The type of switch (conditional expression) and the case constant-expression must be integer type.Control passes to the statement whose case constant expression matches the value of switch(conditional expression). The switch statement can include any number of case statements, but no two case statements within the same switch statement can have the same constant-expression. Execution of the statement body begins at the selected case statement body begins at the selected case statement and proceeds until the end of the body or until a break statement transfers the control out of the body.The break statement is used to end processing of a particular case statement within the switch statement.Program continues to the next case,executing the statements until a break or the end of the statement is reached.The default statement is executed if no case is equal to the value of switch (conditional expression ).If the default statement is omitted, and no case match is found , none of the statements in the switch body are executed.The default statement and is an optional statement and it need not come at the end; it can appear anywhere in the body of the switch statement.The
program to solve the simple arithmetic problem using nested if_else structure if rewritten using switch_case statement as follows:
#include<stdio.h>
void main()
{
int a,b,c,choice;
scanf("%d%d",&a,&b); /*b is not zero*/printf("1.addition\n"); /* option 1 */
printf("2.subtraction\n"); /* option 2 */
printf("3.multiplication\n"); /* option 3 */
printf("4.division\n"); /* option 4 */
scanf("%d",&choice);
switch (choice)
{
case 1:
c=a+b;
printf("%d",c);
break;
case 2:
c=a-b;
printf("%d",c);
break;
case 3:
c=a*b;
printf("%d",c);
break;
case 4:
c=a/b;
printf("%d",c);
break;
default:
printf("The choice is out of range\n");
}
The above program is modular and has easy readability than the program written using nested if_else structure.Consider another example that displays whether the given character is a vowel or consonant.
char ch;
ch='a';
switch(ch)
{
case 'a':
case 'e':
case 'i';
case 'o':
case 'u':printf("the given character is vowel ");
break;
default:printf("the given character is consonant");
}
In the above example as the value of the character ch is 'a',the first case is satisfied and since there is no break statement, the program continues to the next case, executing the statements until a break or the end of the statement is reached.Hence, if the value of the character ch is 'a' or 'e' or 'i' or 'o' or 'u', the statements belongs to the case 'u'have been executed, otherwise, the statement belongs to the default case is executed.
No comments:
Post a Comment