WHAT'S NEW?
Loading...

switch-case Statement in C Programming (Examples)

The switch-case statement is a better way of writing a program with series of if-else ladder occurs. It is used when there are multiple values for the same variables. The switch statement successively tests the value of an expression or variable against a list of integer or character constants. When a match is found, the statement associated with that constant case are executed.
Some rules for switch statement:
  • Value for 'case' must be integer or character constant. 
  • You cannot use range as an expression. 
  • Order of case is not important.
Syntax: 
        switch(expression){
             case value1: 
                   statement block; 
                   break; 
             case value2: 
                   statement block; 
                   break;
             case value3: 
                   statement block; 
                   break;
             ...........
             ...........
             default: 
                   statement block; 
         }                
break statement
This statement causes the program flow to exit from the body of the switch construct. Control goes to the first statement following the end of the switch statement. If the break statement is not use, the control passes to the next case statement and the remaining statements in the switch statement are executed.
Syntax: 
          break; 

default statement
The statements associated with the default keyword are executed if the value of the switch variable does not match any of the case constants.
Syntax: 
     default: 

Example 1: Write a menu base program to calculate addition, subtraction, multiplication and division. 
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
void main(){
 int x, y, ch; 
 printf("Enter First Number : "); 
 scanf("%d",&x); 
 printf("Enter Second Number : ");
 scanf("%d",&y); 
 printf("****** M E N U ******\n"); 
 printf("* 1. Addition       *\n"); 
 printf("* 2. Subtraction    *\n"); 
 printf("* 3. Multiplication *\n");
 printf("* 4. Division       *\n"); 
 printf("*********************\n"); 
 printf("Enter Your Choice !! : "); 
 scanf("%d",&ch); 
 switch(ch){
  case 1: 
   printf("The Sum is %d",x+y); 
   break; 
  case 2:
   printf("The Difference is %d",x-y); 
   break; 
  case 3: 
   printf("The Product is %d",x*y); 
   break; 
  case 4: 
   printf("The Quoitent is %d",x/y); 
   break; 
  default: 
   printf("Invalide Case !!"); 
 }
 getch(); 
}


 Output
Enter First Number : 50
Enter Second Number : 5
****** M E N U ******
* 1. Addition *
* 2. Subtraction *
* 3. Multiplication *
* 4. Division *
*********************
Enter Your Choice !! : 3
The Product is 250

Example 2: WAP a menu base program to calculate Area, Volume and Perimeter. 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<stdio.h>
void main(){
 float l, b, h; 
 int ch; 
 printf("Enter Length : "); 
 scanf("%f",&l); 
 printf("Enter Breadth : ");
 scanf("%f",&b); 
 printf("Enter Height : "); 
 scanf("%f",&h); 
 
 printf("****** M E N U ******\n"); 
 printf(" 1. Calculate Area \n"); 
 printf(" 2. Calculate Volume \n"); 
 printf(" 3. Calculate Perimeter\n\n"); 
 printf("Enter Your Choice !! : "); 
 scanf("%d",&ch); 
 switch(ch){
  case 1: 
   printf("Area = %f",l*b); 
   break; 
  case 2:
   printf("Volume = %f",l*b*h); 
   break; 
  case 3: 
   printf("Perimeter = %f",2*(l+b)); 
   break; 
  default: 
   printf("Invalide Case ! Try Again !!"); 
 }
 getch(); 
}

Output
Enter Length : 15
Enter Breadth : 10
Enter Height : 5
****** M E N U ******
1. Calculate Area
2. Calculate Volume
3. Calculate Perimeter
Enter Your Choice !! : 2
Volume = 750.000000

0 comments:

Post a Comment