The switch..case instruction is a conditional structure that executes different instruction blocks depending on the value of a variable. This instruction is equivalent to nested if instructions.
Syntax of the switch instruction
The switch instruction takes an integer variable as input (integer or int) and executes different blocks of instructions depending on its value. The instruction blocks are placed under case instructions. At each execution, the instruction will compare the val value with the values val1, val2, etc. and execute the corresponding blocks of code.
switch (val) { case val1: // instructions si val=val1 break; case val2: // instructions si val=val2 break; default: // instructions par défaut break; }
An integer is a type of variable. In the C(Arduino) language, integer variables are initialized using the keyword int.
In the following example, a val variable is defined and incremented for each loop. The switch instruction executes the code corresponding to the val value. You will notice that when the val value is not defined in a box, the code under the default instruction is executed.
int val=0; void setup() { Serial.begin(9600); } void loop() { switch (val) { case 0: Serial.println("ZERO"); break; case 1: Serial.println("ONE"); break; case 2: Serial.println("TWO"); break; case 3: Serial.println("THREE"); break; default: Serial.println("UNDEFINED"); break; } val++; if(val>5) val=0; delay(500); }
Auxiliary structures
Use of a char-type variable
The variable to be tested is usually an integer but it can also be a char-type variable. Not to be confused with a string (an array of char or a String).
char val='a'; void setup() { Serial.begin(9600); } void loop() { switch (val) { case 'a': Serial.println("A"); val='b'; break; case 'b': Serial.println("B"); val='d'; break; case 'c': Serial.println("C"); val='a'; break; case 'd': Serial.println("D"); val='c'; break; default: Serial.println("UNDEFINED"); break; } delay(500); }
Gathering of several boxes for an instruction block
By removing the break instruction, it is possible to execute the same instruction block in different cases. This prevents the instruction block from being rewritten when the function is the same in different cases.
char val=0; void setup() { Serial.begin(9600); } void loop() { switch (val) { case 1: case 3: case 5: case 7: case 9: Serial.println("IMPAIR"); break; case 2: case 4: case 6: case 8: Serial.println("PAIR"); break; default: Serial.println("> 10"); break; } val++; if(val>10) val=0; delay(500); }
Things to remember
- the switch instruction takes as input a variable int or char in brackets.
- the switch instruction block is located between braces {}.
- the box instruction takes as input an int or a tank followed by “:”.
- The break instruction must be added after the block of case instruction to avoid executing the following ones
- It is possible to combine several boxes for the same instruction block.
- It is possible to forget the default instruction block.
Bonus
The switch case instruction is often used in combination with enum numerators, which is a type of structure containing constant integers. The enum type is a convenient initialization method for defining a multitude of integers.
enum alphabet{ ALPHA, BETA, GAMMA, UNKNOWN };
In this example, the variables ALPHA, BETA, GAMMA, UNKNOWN will take the values 0,1,2 and 3 respectively. Any variable added following this list will take a value equal to the previous value incremented by 1. Furthermore, it is possible, at any time, to explicitly define the value of the variable and thus change the starting point of the enumeration.
enum alphabet{ ALPHA=0, BETA, GAMMA=9, UNKNOWN };
In this example, the set values are
- ALPHA=0
- BETA=1
- GAMMA=9
- UNKNOWN=10
A numerator can be used to represent a state machine which is a handy method when you want to manage a complex system such as a robot’s algorithm.
enum States{ INITIALIZED, WAITING, FAILED, VALID }; int state=0; void setup() { Serial.begin(9600); } void loop() { switch (state) { case INITIALIZED: Serial.println("INITIALIZED"); break; case WAITING: Serial.println("WAITING"); break; case VALID: Serial.println("VALID"); break; case FAILED: Serial.println("FAILED"); break; default: Serial.println("DEFAULT"); break; } state++; if(state>5) state=0; delay(500); }