C switch statement

In the previous lesson, we discussed if and if…else statements. In this lesson, we talk about the C switch statement. The switch structure is one of the most important and interesting structures in C language, through which one or more statements can be executed according to the value of a variable. This is called “multiple-selection.”
Generally, it is better to use the switch structure in all decisions where there are more than three choices. Note that the switch statement is less powerful than the if statement. This command can only check equality.

The general structure of the switch statement is as follows:

switch(<variable>) {
    case value1:
        //statement
        Break  
    case value2:
        //statement
        Break;
    .
    .
    .
    case value3:
        //statement
        Break;  
    Default:  
         //statement
}

The switch statement consists of a series of case labels, an optional default case, and statements to execute for each case. The “Default” is not necessary for the structure of the switch command and is placed depending on the user’s needs. Each case label checks an equality condition, and if it is equal, the code below it is executed and exits from the switch structure by the Break command. Of course, putting a break is optional.

Switch statement flowchart

Each case can have one or more actions. The switch statement is different from all other control statements in that braces are not required around multiple actions in a case of a switch. The flowchart makes it clear that each break statement at the end of a case causes control to exit the switch statement immediately. The general switch multiple-selection statement (using a break in each case) is
shown in the following image:

c switch statement

If no Break command is placed under each case, after its commands are finished, the next case commands will be executed, and this process will continue until reaching a break and leaving the switch structure. In this case, OR operation is performed between several cases.

Time to practice

Look at the following example:

#include <stdio.h>
int main() {
    int a;
    scanf("%d", &a);
    switch(a){
        case 134:
	        printf("%d\n", a);
	        break;    
        case 200: 
        case 210: 
	        printf("%d\n", ++a);;
	        break;    
        case 250: 
      	        printf("%d\n", --a);;
 	        braek;    
        default:
	        printf("None!");;
    }

    return 0;
}
  1. The variable a is the expression to be checked.
  2. The value of variable ‘a’ is received from the user using scanf().
  3. In the first case, the value of variable a is compared with 134. In the case of equality, the code below is executed, and it is exited from the switch structure by the break command.
  4. In the second and third cases, OR operation is performed between them. That is, if the variable a is equal to the values ​​of 200 or 210, the value of a is incremented and printed on the screen.
  5. In the fourth case, if a is equal to 250, a is decremented and printed on the screen.
  6. If the value of the variable a is not equal to any of the values ​​indicated by the cases, the codes below the Default command will be executed.

Two important points

  1. Multiple switches can be nested. That is, the code under each of the cases can be another switch structure.
  2. The values ​​in switch cases cannot be equal. That is, none of the values ​​<value 1>, <value 2>, and . . . should be equal.

Pay attention to the following example:

#include <stdio.h>

int main() {
	char grade = 'B';
	switch(grade) {	
	   case 'A': 
		  printf("Excellent!\n" );      
		  break;
	   case 'B':  
	   case 'C':  
		  printf("Well done\n");
		  break      
	   case 'D':  
		  printf("You passed\n");
		  break;
	   case 'F':   
		  printf("Better try again\n");
		  break;
	   default:
		  printf("Invalid grade\n");      
	}  
	printf("Your grade is  %c\n", grade);  
	return 0;
}

After compiling and running the program, the following results are obtained:

Well done
Your grade is B

This program is written inside the main() function, and it is supposed to print an appropriate expression on the screen if the character stored in the grade variable is equal to one of the values ​​’A’ to ‘F.’ First, follow the code manually and then see the correctness of the results in the Code Blocks environment.

If you remember the procedure of the previous C language training sessions, you will notice that this example is very similar to them. For this reason, the line-by-line explanation is not given. If you are visiting the site for the first time, be sure to refer to the previous C language training lessons.

Was this helpful?
[0]
Scroll to Top
Scroll to Top