fastread homefastrread library fastread menu

C : C Switch Statement

Tutorial by:      Date: 2016-04-27 04:28:05

❰ Previous Next ❱

The switch statement in C language is used to execute the code from multiple conditions. It is like if else-if ladder statement.

The syntax of switch statement in c language is given below:

 
  1. switch(expression){    
  2. case value1:    
  3.  //code to be executed;    
  4.  break;  //optional  
  5. case value2:    
  6.  //code to be executed;    
  7.  break;  //optional  
  8. ......    
  9.     
  10. default:     
  11.  code to be executed if all cases are not matched;    
  12. }    

Rules for switch statement in C language

1) The switch expression must be of integer or character type.

2) The case value must be integer or character constant.

3) The case value can be used only inside the switch statement.

4) The break statement in switch case is not must. It is optional. If there is no break statement found in switch case, all the cases will be executed after matching the case value. It is known as fall through state of C switch statement.

Let's try to understand it by the examples. We are assuming there are following variables.

 
  1. int x,y,z;  
  2. char a,b;  
  3. float f;  
Valid Switch Invalid Switch Valid Case Invalid Case
switch(x) switch(f) case 3; case 2.5;
switch(x>y) switch(x+2.5) case 'a'; case x;
switch(a+b-2)   case 1+2; case x+2;
switch(func(x,y))   case 'x'>'y'; case 1,2,3;

Flowchart of switch statement in C

flow chart of c switch

Let's see a simple example of c language switch statement.

 
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. switch(number){  
  11. case 10:  
  12. printf("number is equals to 10");  
  13. break;  
  14. case 50:  
  15. printf("number is equal to 50");  
  16. break;  
  17. case 100:  
  18. printf("number is equal to 100");  
  19. break;  
  20. default:  
  21. printf("number is not equal to 10, 50 or 100");  
  22. }  
  23. getch();  
  24. }  

Output

enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50

C Switch statement is fall-through

In C language, switch statement is fall through, it means if you don't use break statement in switch case, all the case after matching case will be executed.

Let's try to understand the fall through state of switch statement by the example given below.

 
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. switch(number){  
  11. case 10:  
  12. printf("number is equals to 10\n");  
  13. case 50:  
  14. printf("number is equal to 50\n");  
  15. case 100:  
  16. printf("number is equal to 100\n");  
  17. default:  
  18. printf("number is not equal to 10, 50 or 100");  
  19. }  
  20. getch();  
  21. }  

Output

enter a number:10
number is equals to 10
number is equals to 50
number is equals to 100
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
number is equals to 100
number is not equal to 10, 50 or 100


❰ Previous Next ❱


C

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website