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

switch (var) { case label_1: // block of code 1: do something when var == label_1 break; case label_2: // block of code 2: do something when var == label_2 break; /* ... more case */ case label_n: // block of code n: do something when var == label_n break; default: // block of code default: do something when var is not equal to any of above label break; }

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:

if (var == label_1) { // block of code 1: do something when var == label_1 } else if (var == label_2) { // block of code 2: do something when var == label_2 /* ... more if else */ } else if (var == label_n) { // block of code n: do something when var == label_n } else { // block of code default: do something when var is not equal to any of above label }

Return Values

  • Nothing

Example Code

void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); for (int i = 0; i < 5; i++) { switch (i) { case 0: Serial.print("Inside case 0: i = "); Serial.println(i); break; case 1: Serial.print("Inside case 1: i = "); Serial.println(i); break; case 2: Serial.print("Inside case 2: i = "); Serial.println(i); break; default: Serial.print("Inside default: i = "); Serial.println(i); break; } } Serial.println("====== TEST END ========"); } void loop() { }

The result on Serial Monitor:

COM6
Send
====== TEST START ====== Inside case 0: i = 0 Inside case 1: i = 1 Inside case 2: i = 2 Inside default: i = 3 Inside default: i = 4 ====== TEST END ========
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ 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.

void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); for (int i = 0; i < 5; i++) { switch (i) { case 0: Serial.print("Inside case 0: i = "); Serial.println(i); break; case 1: Serial.print("Inside case 1: i = "); Serial.println(i); // break; case 2: Serial.print("Inside case 2: i = "); Serial.println(i); break; default: Serial.print("Inside default: i = "); Serial.println(i); break; } } Serial.println("====== TEST END ========"); } void loop() { }

The result on Serial Monitor:

COM6
Send
====== TEST START ====== Inside case 0: i = 0 Inside case 1: i = 1 Inside case 2: i = 1 Inside case 2: i = 2 Inside default: i = 3 Inside default: i = 4 ====== TEST END ========
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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:

void setup() { Serial.begin(9600); for (int i = 0; i < 5; i++) { switch (i) { case 0: Serial.println("HELLO!"); break; case 1: Serial.println("ARDUINO"); break; case 2: Serial.println("ARDUINO"); break; case 3: Serial.println("ARDUINO"); break; default: Serial.println("ArduinoGetStarted.com"); break; } } } void loop() { }

The result on Serial Monitor:

COM6
Send
HELLO! ARDUINO ARDUINO ARDUINO ArduinoGetStarted.com
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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:

void setup() { Serial.begin(9600); for (int i = 0; i < 5; i++) { switch (i) { case 0: Serial.println("HELLO!"); break; case 1: case 2: case 3: Serial.println("ARDUINO"); break; default: Serial.println("ArduinoGetStarted.com"); break; } } } void loop() { }

The result on Serial Monitor:

COM6
Send
HELLO! ARDUINO ARDUINO ARDUINO ArduinoGetStarted.com
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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:

void setup() { Serial.begin(9600); for (int i = 0; i < 4; i++) { switch (i) { case 0: Serial.println("Hello!"); break; case 1: Serial.print("Let's learn "); Serial.println("Arduino"); break; case 2: Serial.println("Arduino"); break; default: Serial.println("via ArduinoGetStarted.com"); break; } } } void loop() { }

The result on Serial Monitor:

COM6
Send
Hello! Let's learn Arduino Arduino via ArduinoGetStarted.com
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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:

void setup() { Serial.begin(9600); for (int i = 0; i < 4; i++) { switch (i) { case 0: Serial.println("Hello!"); break; case 1: Serial.print("Let's learn "); case 2: Serial.println("Arduino"); break; default: Serial.println("via ArduinoGetStarted.com"); break; } } } void loop() { }

The result on Serial Monitor:

COM6
Send
Hello! Let's learn Arduino Arduino via ArduinoGetStarted.com
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES