break wird benutzt, um aus for, while oder do...while-Schleifen zu springen, wobei die normalen condition übersprungen werden. Es wird auch benutzt, um aus switch case-Statements zu springen.
Syntax
while (condition) {//Statement(s)break;//Statement(s)}
Parameter
Keine.
Rückgabewert
Nichts.
Beispielcode
Example with For Loop
Beispielcode 1
The following code exits the for loop when the i greater than 3
voidsetup() {Serial.begin(9600);Serial.println("====== TEST START ======");for (int i = 1; i < 1000; i++) {Serial.print("Inside the FOR loop: i = ");Serial.println(i);if(i > 3)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 the FOR loop: i = 1
Inside the FOR loop: i = 2
Inside the FOR loop: i = 3
====== TEST END ========
Ln 11, Col 1
Arduino Uno on COM15
2
Beispielcode 2
Der folgende Code springt aus der for-Schleife, wenn der Sensorwert den Threshold übersteigt.
// Iteriere über die Zahlen von 0 bis 255for (int x = 0; x < 255; x++) {// Schreibe auf den LED-PinanalogWrite(PWMpin, x);// Lies den Sensorwert ein sens = analogRead(sensorPin);// Wenn der Wert größer als der Threshold istif (sens > 40){// Setze x auf 0 x = 0;// Springe rausbreak; }// Warte 50 Millisekundendelay(50);}
Example with While Loop
The following code exits the while loop when the i greater than 3
voidsetup() {Serial.begin(9600);Serial.println("====== TEST START ======");int i = 0;while (true) { // while(true) means loop foreverSerial.print("Inside the WHILE loop: i = ");Serial.println(i);if(i > 3)break; i++; // increase i by 1 }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 the WHILE loop: i = 0
Inside the WHILE loop: i = 1
Inside the WHILE loop: i = 2
Inside the WHILE loop: i = 3
====== TEST END ========
Ln 11, Col 1
Arduino Uno on COM15
2
Example with Do While Loop
The following code exits the while loop when the i greater than 3
voidsetup() {Serial.begin(9600);Serial.println("====== TEST START ======");int i = 0;do {Serial.print("Inside the DO WHILE loop: i = ");Serial.println(i);if(i > 3)break; i++; // increase i by 1 } while (true); // while(true) means loop foreverSerial.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 the DO WHILE loop: i = 0
Inside the DO WHILE loop: i = 1
Inside the DO WHILE loop: i = 2
Inside the DO WHILE loop: i = 3
====== TEST END ========
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.