Arduino Programming Language: SwitchCase

Name

switch

Description

Works like an if else structure, but is more conveniente when you need to select between three or more alternatives. 
Program controls jumps to the case with the same value as the expression. 
The break keyword exits the switch statement, and is typically used at the end of each case. 
Without it, the switch statement will continue executing the following expressions until a break, or the end of the switch statement is reached.

Syntax

switch (expression) {
case label:
// statements
break;
case label:
// statements
break;
default:
// statements
}

Parameters

  • expression: byte, char, or int
  • label: byte, char, or int
  • statements: one or more statements to be executed

Usage

Application

Examples

switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
//do something when var equals 2
break;
default:
// if nothing else matches, do the default
// default is optional
}


Based on the reference originally written by Arduino Community,  wiring.org.co and  arduino.cc