Switch case

From Microduino Wiki
Revision as of 07:04, 4 August 2016 by Fengfeng (talk) (Created page with " *'''switch / case''' Same with if statement, switch…case controls the process of the program through the code the programmer set to be executed under different conditions....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
  • switch / case

Same with if statement, switch…case controls the process of the program through the code the programmer set to be executed under different conditions. Specially, switch statement makes a comparison between the value of the variables and the one set in case statement. When the setting value in case statement is same with the value of the variable, this case statement will be executed.

Keyword break can be sued to exit a switch statement. Each case statement usually ends in break. If there is no break statement, switch statement will execute the following statements continually(all the way down)until meets a break or the end of the switch statement.

    • Example

switch (var) {
case 1:
  //execute some statements when the var is equal to 1
  break;
case 2
  //execute some statements when the var is equal to 2
  break;
default: 
  //if there is no match, execute the default
  //default is not essential
}
    • Syntax

switch (var) {
case label:
  //declaration
  break;
case label:
  // declaration
  break;
default: 
  // declaration
}

Parameters var: variable used to be compared with the label in the following case

label: value used to be compared with the variable


[Return to Arduino Syntax Manual]