Switch Case statements
Just like If statements, switch case statements help the control and flow of the programs. Switch case's allow you to make a list of "cases" inside a switch bracket in which arduino will find the most suitable and run it.
Parameters
- var - variable you wish to match with case statements
- default - if no other conditions are met, default will run
- break - important, without break, the switch statement will continue checking through the statement for any other possibilities. If one is found, it will run that as well, which may not be your intent. Break tells the switch statement to stop looking for matches, and end its function.
Example
switch (var) {
case 1:
//do something when var == 1
break;
case 2:
//do something when var == 2
break;
default:
// if nothing else matches, do the default
}
Reference Home