Switch statement


Switch statement
When one of many alternatives is to be selected, in general we can use else-if ladder statement which is two-way decision statement. However, complexity of such program increases when the number of alternatives increases. To avoid this complexity C has multiway decision statement known as “switch” statement.
A switch statement which is a selection statement that lets you transfer control to different statements within the switch body depending on the value of the switch expression. The switch expression must evaluate to an integer value. The body of the switch statement contains case clauses that consist of
  • A case label
  • An optional default label
  • A case expression
  • A list of statements.
If the value of the switch expression equals the value of one of the case expressions, the statements following that case expression are processed. If not, the default label statements are processed. Each case clause and default clause can contain statements. The default is optional.
Syntax:

switch(expression)
{
                        case label-1: statements-1;
                                                break;
                        case label-2: statements-2;
                                                break;
                                    .
                                    .
                        case label-n: statements-n;
                                                break;
                        default        : statements-d;
                                                break;
}

The switch statement tests the value of given expression or variable with the list of case labels and when a match occurs, the corresponding block of statements will be executed and switch will be terminated. If no match found, default statements will be executed if at all default is present. Here the expression and labels must be either int or char datatype.




Here exp is the result of expression or simple variable which will be matched with labels.
Notes:-
1.     The Expression is an integer expression or it can be character.
2.     label-1,label-2,…label-n are known as case labels and these can be either constant or constant expressions and they can be either int or char.
3.     Each these case labels should be unique with in the switch case.
4.     Statements-1, Statements-2,…. are statement lists can contain zero or more statements.
5.     there is no need to keep braces around these statement blocks.
6.     case lebels must end with colon(:).
7.     The break statement at end of each statement block indicates the end of particular case and causes an exit from switch statement.
8.     The default is optional case.
9.     It is possible to nest the switch statement i.e. switch can be part of “case” statement.
10.  break statement is not necessary for each case.

Sample program for student grade depending on their average marks
#include<stdio.h>
#include<conio.h>
void main()
{
 int avg,grade;
 clrscr();
 printf("\n Enter avg: ");
 scanf("%d",&avg);
 grade=avg/10;
 switch(grade)
 {
  case 10:
  case 9 :
  case 8 :
  case 7 : printf("Distnction");
               break;
  case 6 : printf("First Class");
               break;
  case 5 : printf("Second Class");
               break;
  case 4 : printf("Third Class");
               break;
  case 3 :
  case 2 :
  case 1 :
  case 0 : printf("Fail");
               break;
  default: printf("Enter correct average(0 to 100)..");
               break;
 }
 getch();
}

Output

 Enter avg: 98
Distnction
Sample program which takes two integer operands and operator from user and performs arithmetic operations and prints the result.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 int a,b,c;
 char op;
 clrscr();
 printf("\n Enter a,b: ");
 scanf("%d%d",&a,&b);
 printf(" Enter Operator (+,-,*,/,%): ");
 op=getche();
 switch(op)
 {
  case '+': c=a+b; break;
  case '-': c=a-b; break;
  case '*': c=a*b; break;
  case '/': c=a/b; break;
  case '%': c=a%b; break;
  default : printf("\n Enter correct operator..");
                getch();
                exit(0);
 }
 printf("\n Result: %d",c);
 getch();
}

Output

 Enter a,b: 5 6
 Enter Operator (+,-,*,/,%): +
 Result: 11

 Enter a,b: 5 6
 Enter Operator (+,-,*,/,%): x
 Enter correct operator..



No comments:

Post a Comment