continue

Descrição

O comando continue "pula" o resto da iteração atual de um loop (for, while, ou do...while). Esse comando continua a checar a expressão condicional do loop, e procede com qualquer iterações subsequentes.

Código de Exemplo

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

O código abaixo escreve o valor de 0 a 255 ao pino PWMpin, mas pula os valores no intervalo 41 a 119.

for (int x = 0; x <= 255; x ++) { if (x > 40 && x < 120) { // cria um salto nos valores continue; } analogWrite(PWMpin, x); delay(50); }

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  

Ver Também

ARDUINO BUY RECOMMENDATION

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

※ OUR MESSAGES