do...while funktioniert genauso wie eine while-Schleife. Der einzige Unterschied ist, dass do...while immer mindestens einmal ausgeführt wird, da die Bedingung erst am Schluss der Schleife getestet wird.
Syntax
do {//Statement(s)} while (condition);
Parameter
condition: Ein Ausdruck, der auf true oder false evaluiert.
Rückgabewert
Nichts.
Beispielcode
Beispielcode 1
voidsetup() {Serial.begin(9600);Serial.println("====== TEST START ======");int i = 0;do {Serial.print("Inside the DO WHILE loop: i = ");Serial.println(i); i++; // increase i by 1 } 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 = 0
Inside the DO WHILE loop: i = 1
Inside the DO WHILE loop: i = 2
Inside the DO WHILE loop: i = 3
Inside the DO WHILE loop: i = 4
====== TEST END ========
Ln 11, Col 1
Arduino Uno on COM15
2
Beispielcode 2
Liest in do-while-Schleife 100 Mal einen Sensorwert aus.
// Initialisiere x mit Wert 0int x = 0;do {delay(50); // Warte 50 Millieskunden, bis der Sensor wieder einen stabilen Wert liefert. x = readSensor(); // Lies den Sensorwert x++; //Inkrementiere die Zählervariable} while (x < 100); // Wiederhole das ganze 100 Mal
do...while loop vs while loop
The while loop checks the condition before executing the block of code; conversely, the dowhile loop checks the condition after executing the block of code. Therefore, the dowhile loop will always be executed at least once, even if the condition is false at the beginning.
The do...while and while loop are the same, except for the case in which the condition is false at the beginning.
For example:
Code with do...while loop
voidsetup() {Serial.begin(9600);Serial.println("====== TEST START ======");int i = 10;do {Serial.print("Inside the DO WHILE loop: i = ");Serial.println(i); i++; // increase i by 1 } while (i < 5);Serial.println("====== TEST END ========");}voidloop() {}
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 = 10
====== TEST END ========
Ln 11, Col 1
Arduino Uno on COM15
2
Code with while loop
voidsetup() {Serial.begin(9600);Serial.println("====== TEST START ======");int i = 10;while (i < 5) {Serial.print("Inside the DO WHILE loop: i = ");Serial.println(i); i++; // increase i by 1 }Serial.println("====== TEST END ========");}voidloop() {}
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 ======
====== TEST END ========
Ln 11, Col 1
Arduino Uno on COM15
2
※ Anmerkungen und Warnungen:
There are three ways to escape the dowhile loop:
The condition of the do while loop becomes false.
The execution of the code reaches a break statement inside the loop.
The execution of the code reaches a goto statement inside the loop, which jumps to a label located outside of the loop.
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.