Boucle do - while

Description

La boucle do / while ("faire tant que" en anglais) fonctionne de la même façon que la boucle while, à la différence près que la condition est testée à la fin de la boucle, et par conséquent la boucle do sera toujours exécutée au moins une fois.

Syntaxe

do // faire... { // bloc d'instruction } while (condition); // tant que la condition est vraie

Paramètres

  • condition: expression booléenne dont le résultat peut être VRAI ou FAUX.

Exemple

Exemple 1

void setup() { 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 ========"); } void loop() { }

The result on Serial Monitor:

COM6
Send
====== 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 ========
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Exemple 2

int x = 0; do // faire... { delay(50); // attendre la stabilisation du capteur x = readSensors(); // lit la valeur de la tension du capteur } while (x < 100); // ...tant que x est inférieur à 100

do...while loop vs while loop

The while loop checks the condition before executing the block of code; conversely, the do while loop checks the condition after executing the block of code. Therefore, the do while 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
void setup() { 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 ========"); } void loop() { }
COM6
Send
====== TEST START ====== Inside the DO WHILE loop: i = 10 ====== TEST END ========
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Code with while loop
void setup() { 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 ========"); } void loop() { }
COM6
Send
====== TEST START ====== ====== TEST END ========
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ Remarque:

There are three ways to escape the do while 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.

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

※ OUR MESSAGES