Das continue-Statement überspringt die restliche Iteration einer Schleife (for, while oder do...while). Das continue-Statement checkt anschließend die condition und durchläuft die Schleife erneut, bis die Abbruchbedingung erreicht ist.
Syntax
while (condition) {//Statement(s)continue;//Statement(s)}
Parameter
Keine.
Rückgabewert
Nichts.
Beispielcode
Example with For Loop
Example 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 < 5; i++) {Serial.print("Inside the FOR loop: i = ");Serial.print(i);Serial.println(", start");if(i > 2)continue;Serial.print("Inside the FOR loop: i = ");Serial.print(i);Serial.println(", end"); }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, start
Inside the FOR loop: i = 1, end
Inside the FOR loop: i = 2, start
Inside the FOR loop: i = 2, end
Inside the FOR loop: i = 3, start
Inside the FOR loop: i = 4, start
====== TEST END ========
Ln 11, Col 1
Arduino Uno on COM15
2
Example 2
Der Code schreibt die Werte von 0 bis 255 auf den LED-Pin, aber überspringt Werte zwischen 41 und 119.
// Iteriere über die Werte von 0 bis 255for (int x = 0; x <= 255; x ++) {// Springe bei den Werten zwischen 40 und 120 weiterif (x > 40 && x < 120){continue; }// Schreibe den Wert auf den LED-PinanalogWrite(PWMpin, x);// Warte 50 Millieskundendelay(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 (i < 5) { i++; // increase i by 1Serial.print("Inside the WHILE loop: i = ");Serial.print(i);Serial.println(", start");if(i > 2)continue;Serial.print("Inside the WHILE loop: i = ");Serial.print(i);Serial.println(", end"); }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 = 1, start
Inside the WHILE loop: i = 1, end
Inside the WHILE loop: i = 2, start
Inside the WHILE loop: i = 2, end
Inside the WHILE loop: i = 3, start
Inside the WHILE loop: i = 4, start
====== 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 { i++; // increase i by 1Serial.print("Inside the DO WHILE loop: i = ");Serial.print(i);Serial.println(", start");if(i > 2)continue;Serial.print("Inside the WHILE loop: i = ");Serial.print(i);Serial.println(", end"); } while (i < 5);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 DO WHILE loop: i = 1, start
Inside the DO WHILE loop: i = 1, end
Inside the DO WHILE loop: i = 2, start
Inside the DO WHILE loop: i = 2, end
Inside the DO WHILE loop: i = 3, start
Inside the DO WHILE loop: i = 4, start
====== 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.