Reference for Wiring 1.0 (ALPHA) 0017+. If you have a previous version, use the reference included with your software.
If you see any errors or have any comments, let us know.
Name
switch()
Examples
int num = 1;
switch(num) {
case 0:
Serial.println("Zero"); // Does not execute
break;
case 1:
Serial.println("One"); // Prints "One"
break;
}
char letter = 'N';
switch(letter) {
case 'A':
Serial.println("Alpha"); // Does not execute
break;
case 'B':
Serial.println("Bravo"); // Does not execute
break;
default: // Default executes if the case labels
Serial.println("None"); // don't match the switch parameter
break;
}
// Removing a "break" enables testing
// for more than one value at once
char letter = 'b';
switch(letter) {
case 'a':
case 'A':
Serial.println("Alpha"); // Does not execute
break;
case 'b':
case 'B':
Serial.println("Bravo"); // Prints "Bravo"
break;
}
Description
Works like an if else structure, but switch() is more convenient when you need to select between three or more alternatives. Program controls jumps to the case with the same value as the expression. All remaining statements in the switch are executed unless redirected by a break. Only primitive datatypes which can convert to an integer (byte, char, and int) may be used as the expression parameter. The default is optional.
if else 구조처럼
동작합니다. 그러나 switch()
가 2 개 이상을 선택할 때
편리합니다. switch 문에서
모든 남아있는 문은 break
로 재지정됩니다. 단지
원시 데이터 형이
정수로 변환될수
있으며 expression 파라메터로
사용될 수 있습니다. default
는 선택사양 입니다.
Syntax
switch(expression)
{
case label:
statements
case label: // Optional
statements // "
default: // "
statements // "
}