Wie auch if-Statements, erlaubt es auch switchcase, dass abhängig von der Bedingung in verschiedenen Situationen unterschiedlicher Code ausgeführt wird. Im Detail vergleicht switchcase die Variablenwerte mit denen in den case-Statements. Wenn ein passendes case-Statement gefunden wird, so wird der Code in diesem case-Statement ausgeführt.
Das break-Keywort beendet das switchcase-Statement und wird üblicherweise am Ende jedes case-Statements verwendet. Wenn kein break-Keywort verwendet wird, führt switchcase alle Statements aus, bis ein break-Keywort auftaucht oder das switchcase zu Ende ist.
Syntax
switch (var) {case label1:// Statement(s)break;case label2:// Statement(s)break;default:// Statement(s)break; // Wird nicht benötigt, wenn Statement(s) vorhanden sind}
Parameter
var: Eine Variable, die mit den eizelnen Fällen verglichen werden sollen. Erlaubte Datentypen: int, char.
voidsetup() {Serial.begin(9600);Serial.println("====== TEST START ======");for (int i = 0; i < 5; i++) {switch (i) {case 0:Serial.print("Inside case 0: i = ");Serial.println(i);break;case 1:Serial.print("Inside case 1: i = ");Serial.println(i);break;case 2:Serial.print("Inside case 2: i = ");Serial.println(i);break;default:Serial.print("Inside default: i = ");Serial.println(i);break; } }Serial.println("====== TEST END ========");}voidloop() {}
Das ergebnis am seriellen monitor:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
====== TEST START ======
Inside case 0: i = 0
Inside case 1: i = 1
Inside case 2: i = 2
Inside default: i = 3
Inside default: i = 4
====== TEST END ========
Ln 11, Col 1
Arduino Uno on COM15
2
※ Anmerkungen und Warnungen:
if we does not use the break in a case, the next case will also be executed. The execution is stopped when it reaches a break. Let's comment or remove a break in the above example and see the result.
voidsetup() {Serial.begin(9600);Serial.println("====== TEST START ======");for (int i = 0; i < 5; i++) {switch (i) {case 0:Serial.print("Inside case 0: i = ");Serial.println(i);break;case 1:Serial.print("Inside case 1: i = ");Serial.println(i);// break;case 2:Serial.print("Inside case 2: i = ");Serial.println(i);break;default:Serial.print("Inside default: i = ");Serial.println(i);break; } }Serial.println("====== TEST END ========");}voidloop() {}
Das ergebnis am seriellen monitor:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
====== TEST START ======
Inside case 0: i = 0
Inside case 1: i = 1
Inside case 2: i = 1
Inside case 2: i = 2
Inside default: i = 3
Inside default: i = 4
====== TEST END ========
Ln 11, Col 1
Arduino Uno on COM15
2
As you can see, when i = 1, the code run through case 1 and case 2.
Advanced Usage
If we miss a break in a case by accident, it results in the wrong operation of the code. Why the language does not force to use break instead of optional? Let's see two advanced usages to know the reason why.
Advanced Usage 1
Let's see the below example:
voidsetup() {Serial.begin(9600);for (int i = 0; i < 5; i++) {switch (i) {case 0:Serial.println("HELLO!");break;case 1:Serial.println("ARDUINO");break;case 2:Serial.println("ARDUINO");break;case 3:Serial.println("ARDUINO");break;default:Serial.println("ArduinoGetStarted.com");break; } }}voidloop() {}
Das ergebnis am seriellen monitor:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
As you can see, the results of both example codes are the same. But the second example code are shorter. Furthermore, if you get familiar to the second example code, you will realize that the second code is more readable. That is the benefit of not using the break;
Advanced Usage 2
Let's see the below example:
void setup() { Serial.begin(9600); for (int i = 0; i < 4; i++) { switch (i) { case 0: Serial.println("Hello!"); break; case 1: Serial.print("Let's learn "); Serial.println("Arduino"); break; case 2: Serial.println("Arduino"); break; default: Serial.println("via ArduinoGetStarted.com"); break; } }}void loop() {}
Das ergebnis am seriellen monitor:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Hello!
Let's learn Arduino
Arduino
via ArduinoGetStarted.com
Ln 11, Col 1
Arduino Uno on COM15
2
In the above code, you can see the case 1 and 2 have the same a line of code (Serial.println("Arduino")). Let's compare with the below code:
void setup() { Serial.begin(9600); for (int i = 0; i < 4; i++) { switch (i) { case 0: Serial.println("Hello!"); break; case 1: Serial.print("Let's learn "); case 2: Serial.println("Arduino"); break; default: Serial.println("via ArduinoGetStarted.com"); break; } }}void loop() {}
Das ergebnis am seriellen monitor:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Hello!
Let's learn Arduino
Arduino
via ArduinoGetStarted.com
Ln 11, Col 1
Arduino Uno on COM15
2
As you can see, the results of both example codes are the same. However, the second example code are shorter. That is another benefit of not using the break;
Bitte Beachten: Dies sind Partnerschaftslinks. Wenn Sie die Komponenten über diese Links Kaufen, können wir eine Provision erhalten, ohne weitere Kosten für Sie. Wir Schätzen es.