continue

Description

L'instruction continue est utilisée pour passer outre certaines portions de code dans les boucle do, for ou while. Cela force le test de la condition sans exécuter le code restant de la boucle.

Syntaxe

continue;

Exemple

Example with For Loop

Example 1

The following code exits the for loop when the i greater than 3

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

The result on Serial Monitor:

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

Example 2

The following code writes the value of 0 to 255 to the PWMpin, but skips the values in the range of 41 to 119.

for (x = 0; x < 255; x ++) // boucle for qui compte x de 0 à 255 { if (x > 40 && x < 120) { // crée un saut en fonction de la valeur de x continue; // l'instruction continue force à continuer // la boucle sans exécuter les instructions } // les instructions suivantes sont ignorées // lorsque continue; est prise en compte digitalWrite(PWMpin, x); // met une impulsion de largeur x sur la broche delay(50); // pause de 50ms }

Example with While Loop

The following code exits the while loop when the i greater than 3

void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); int i = 0; while (i < 5) { i++; // increase i by 1 Serial.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 ========"); } void loop() { }

The result on Serial Monitor:

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

Example with Do While Loop

The following code exits the while loop when the i greater than 3

void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); int i = 0; do { i++; // increase i by 1 Serial.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 ========"); } void loop() { }

The result on Serial Monitor:

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

Voir Également

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES

  • We are AVAILABLE for HIRE. See how to hire us to build your project
  • Any suggestion, correction, and translation? please email us at ArduinoGetStarted@gmail.com, We appreciate it
  • We mainly keep improving the references in English. See English version of this page for the latest update.