Switch case statement in ‘C’
In this article, let’s discuss the switch/case statement.
Switch case statement in ‘C’
- Switch/case statement is one of the decision making statement available in ‘C’.
- It may be used instead of several if-else statements.
Syntax of switch/case statement
Let’s understand the syntax of the switch/case statement. It is shown in Figure 1.
A switch/case statement is also used to take some decisions based on the expression evaluation.
How does the switch/case work?
First, the expression will be evaluated, and after that, it is compared with several cases.
A switch case body is a collection of several cases and a default case.
So, the default statement is optional. When none of the cases are true or matched with the expression evaluation, then the default case will be evaluated.
Now let’s examine this switch/case block.
Please note that switch, case, break, default are all reserved keywords of C standard.
First, the expression will be evaluated in the switch statement. The body of the switch statement is shown in Figure 1. Inside the body of the switch statement, you can have many case statements or case blocks and a default block.
The expression is evaluated, and based on the final result of the expression, a case block will be executed.
Case label, and it must be an integer value, remember. So, the case must be followed by an integer value, and after that, you have to write a colon(:).
After that, for that case, you can write as many numbers of statements.
And once all the statements are executed, you can break the switch statement by using a break keyword.
When the break is executed, the control comes out of the switch body, and it continues with the rest of the code.
Example
int main(void) { uint8_t key_read = read_keypad(); switch(key_read) { case 1: all_leds_race(); break; case 2: all_leds_on(); break; case 3: all_leds_toggle(); break; case 4: all_leds_blink(); break; default: all_leds_off(); printf("Invalid key! Please enter number between (1 to 4) only\n"); }
Here is a program which reads a keypad, and based on the key_read, the decision will be taken. This read_keypad function returns the value of the key that the user has pressed. The user presses key 2, so read_keypad returns 2. 2 is stored in the key_read variable. And that key _read variable is used as an expression in the switch statement. So, the value of key_read is 2. That’s why the key_read value is compared with the values of the case statements. That’s why, in this case, if key_read is 2, then control directly comes to case 2. It doesn’t execute case 1, it directly goes to case 2.
In case 2, it executes the statements of case 2. In this case all_leds_on() is a function call. Once this function is executed, the program encounters a break. So, once a break is encountered, the control immediately breaks this switch statement body, and it moves out of the switch body and continues for the rest of the code of the program.
So, if key_read is 2 and break is not present in case 2 statements, in that case, the switch body will not be broken. It continues with the case 3 statement and then finds a break. And once it finds a break, then it breaks the switch body and goes out of the switch body.
Sometimes we use a break, and sometimes we don’t use a break, so that depends upon your logic. But not using a break statement is fine; that is not actually a compilation error. Carefully use the break that’s according to your logic, whether to use break or not to use a break.
If the key_read value is 5, then none of these cases match, the control directly goes to the default case, and the statements in the default case will be executed. For a default case, you need not mention the break statement because it anyway comes at the end. So, you should always write the default case at the end, and it goes out of the switch body.
You got the idea behind the switch/case statement, and it will be more clear when we do one exercise. I will do the exercise in the following article.
Get the Microcontroller Embedded C Programming Full Course on Here.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1