Arduino

Switch case statement in Arduino

In Arduino programming the switch case statements are also used to regulate the flow of the code using conditional statements. The switch case statement can be used when it is needed to choose from more than two options. This write up briefly explains the switch case statement with the help of a flow chart and multiple Arduino examples.

Switch case statement

In the switch case statement if the case is true, then the statement will be executed, and output will be displayed and if the case is false the code will move to the next case. The code will check all the cases given by the user. If all the cases are false, then there is a default case that is declared at the end of the Arduino program will be executed. To use the switch case statement the following syntax must be kept in mind:

switch (variable) {
   case Variable value:
   // instruction
   break;

case Variable value:  
   // instruction
   break;

default:
   // instruction
   break;
}

To use the switch case statement first the variable is to be declared upon whose value the cases are to be made, then the cases are written by numbering them with the values of the variable that are required by the user in the output. By giving the case number the required value will check if the operation applied at the start of the Arduino program is giving the desired value or not. To isolate cases from each other a keyword break is used at the end of each case. The working of the switch case statement can be further understandable by the flow chart.

Example codes for the switch-case statement

The switch case statement can be used for multiple purposes like to check the values obtained by performing any mathematical operation, generating a list of numbers having specific intervals or assign any instruction based on the values obtained from any type of operation. This context gives the two different types of example codes of Arduino programming in which switch case statements are used. This will assist the reader to easily grasp the concept of switch-case statement and its implementation in Arduino programming.

Example 1 for the switch-case statement in Arduino

The first example code is about plotting a series of numbers in incremental form from 1 to 4 with the help of a for loop and switch case statement. The series can also be plotted in descending form just by changing the increment operator to decrement operator.

void setup() {
       Serial.begin(9600);
       Serial.println("====== series of numbers from 0 to 4 ======");
  for (int a = 0; a <= 4; a++) {
    switch (a) {
      case 0:
        Serial.print(" case 0: a = ");
        Serial.println(a);
        break;
      case 1:
        Serial.print("case 1: a = ");
        Serial.println(a);
        break;
      case 2:
        Serial.print(" case 2: a = ");
        Serial.println(a);
        break;
        case 3:
        Serial.print(" case 3: a = ");
        Serial.println(a);
        break;
        case 4:
        Serial.print("case 4: a = ");
        Serial.println(a);
        break;
      default:
        Serial.print(" default case : a = ");
        Serial.println(a);
        break;
    }
  }
}
void loop() {
}

Output

Example 2 for the switch- case statement

In the second example of Arduino code a mathematical operation is performed and then the output of the operation is tested using different cases on each iteration of the loop where two values are considered. One is the variable value and the other is the constant value. The value of the variable a c will get changed after each iteration and the value of integer d is kept constant throughout the Arduino program.

void setup() {
       Serial.begin(9600);
       Serial.println("====== multiplication ======");
       const int d = 2;
       int a;
  for (int c = 0; c <= 4; c++){
    a= c*d;
    switch (a) {
      case 0:
        Serial.print(" case 0: a = ");
        Serial.println(a);
        break;
      case 1:
        Serial.print("case 1: a = ");
        Serial.println(a);
        break;
       case 2:
        Serial.print(" case 2: a = ");
        Serial.println(a);
        break;
        case 3:
        Serial.print(" case 3: a = ");
        Serial.println(a);
        break;
        case 4:
        Serial.print("case 4: a = ");
        Serial.println(a);
        break;
        case 5:
        Serial.print("case 5: a = ");
        Serial.println(a);
        break;
        case 6:
        Serial.print("case 6: a = ");
        Serial.println(a);
        break;
      default:
        Serial.print(" default case : a = ");
        Serial.println(a);
        break;
  }
  }
}
void loop() {
}

Output

The output displays only the cases on which the value for variable a for the multiplication operation is true. Similarly, there is one default case that shows the value that is obtained after the case 6 is executed.

Conclusion

The switch case statements use multiple conditions in the form of cases. Each condition has its own instruction and each case is separated using the break keyword. In this write-up switch-case statement is explained with the help of a flow chart and example codes.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.