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.
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:
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.
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;
}
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.