switch...case
Description
Like if statements, switch case controls the flow of programs by allowing programmers to specify different code that should be executed in various conditions. In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run.
The break keyword exits the switch statement, and is typically used at the end of each case. Without a break statement, the switch statement will continue executing the following expressions ("falling-through") until a break, or the end of the switch statement is reached.
Syntax
Keywords
There are four keywords used with switch ... case: switch, case, break, and default. Among them, break and default are optional.
If the default keyword is used, it must be placed after all of case, and it does not need a break.
Parameter Values
- var: a variable whose value to compare with various cases. Allowed data types: int, char.
- label_1, label_2,..., label_n : constants. Allowed data types: int, char.
How it works
- The value of the expression (var) is compared with the values of each case (label_)
- If there is a match, the associated block of code is executed.
- The default keyword specifies a block of code to be run if there is no case match. Obviously, If the default is not used and there is no case match, the switch ... case does not run any block of code.
The above switch ... case syntax is equivalent to the below code when using if ... else:
Return Values
- Nothing
Example Code
The result on Serial Monitor:
※ NOTES AND WARNINGS:
if we does not use the break in a case, the next case will also be executed. The execution is stopped when it reaches a break. Let's comment or remove a break in the above example and see the result.
The result on Serial Monitor:
As you can see, when i = 1, the code run through case 1 and case 2.
Advanced Usage
If we miss a break in a case by accident, it results in the wrong operation of the code. Why the language does not force to use break instead of optional? Let's see two advanced usages to know the reason why.
Advanced Usage 1
Let's see the below example:
The result on Serial Monitor:
In the above code, you can see the case 1, 2 and 3 have the same code (do the same work). Let's remove all code in the case 1 and case 2:
The result on Serial Monitor:
As you can see, the results of both example codes are the same. But the second example code are shorter. Furthermore, if you get familiar to the second example code, you will realize that the second code is more readable. That is the benefit of not using the break;
Advanced Usage 2
Let's see the below example:
The result on Serial Monitor:
In the above code, you can see the case 1 and 2 have the same a line of code (Serial.println("Arduino")). Let's compare with the below code:
The result on Serial Monitor:
As you can see, the results of both example codes are the same. However, the second example code are shorter. That is another benefit of not using the break;
See Also
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |