Tags: ,

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);
}

Things to remember

  • the switch instruction takes as input an integer variable
  • the condition is between brackets
  • the switch instruction block is between brace {}
  • the case instruction takes as input an integer variable followed by “:”
  • The break instruction must be added after each case 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);
}

Sources