Control Structure
Lesson 25 - Using continue Statement
Learn how continue skips the current loop iteration and moves directly to the next cycle.
Progress indicator
Lesson 25 of 28
Learning Objectives
- Understand what continue does inside loops.
- Write correct continue syntax.
- Explain how continue skips only the current iteration.
- Use continue in for and while loops safely.
- Compare continue and break clearly.
- Trace internal flow for skipped and non-skipped iterations.
Concept Explanation
What is continue
continue skips the rest of the current loop iteration and jumps to the next iteration.
It does not exit the loop completely; it only skips one cycle.
continue Syntax
continue;How continue Works
- Loop enters a cycle.
- Condition for skip is checked.
- If true,
continue;runs. - Remaining statements in that cycle are skipped.
- Loop proceeds to next iteration.
| i Value | Condition | Result |
|---|---|---|
| 0,1,2,4,5,7 | false | Run LED blink code |
| 3,6 | true | continue runs, blink skipped |
continue in for Loop
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
Serial.println(i);
}continue in while Loop
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
Serial.println(i);
}continue vs break (Comparison)
continue: skip only current iteration.break: exit loop completely.- Use continue when you want loop to keep running after skipping one case.
Use break when the whole loop should stop; use continue when only one cycle should be ignored.
When to Use continue
- Skip invalid sensor samples but continue reading others
- Ignore a specific counter value in demo loops
- Skip special cases while processing arrays
Example Code
This example skips blink cycles 3 and 6 using continue.
const int ledPin = 2;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
for (int i = 0; i < 8; i++) {
if (i == 3 || i == 6) {
Serial.print("Skipping cycle: ");
Serial.println(i);
continue;
}
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
Serial.print("Ran cycle: ");
Serial.println(i);
}
delay(1000);
}Example Code Explanation
- Loop is set to iterate from
i = 0toi < 8. - When
iis 3 or 6, continue runs and skips LED blinking lines. - For other values, LED ON/OFF sequence runs normally.
- Serial output shows which cycles were skipped and which were executed.
Real-life example
In a sensor list, continue can skip faulty readings and keep processing the remaining samples.
What Happens Inside
- Condition check happens first in each iteration.
- At skip values, continue jumps directly to loop update/next iteration.
- No LED operations run in skipped iterations.
- Non-skipped iterations execute full blink logic.
- Loop completes all iterations unless break/condition exit happens.
Internal flow rule
check skip condition -> continue or run body -> next iteration
Common Mistakes with continue
- Expecting continue to exit the whole loop (it does not).
- Using continue before required state updates in while loops.
- Placing important logic below continue and accidentally skipping it.
- Overusing continue and making control flow hard to follow.
Best Practices for continue
- Use clear skip conditions and comment why skip happens.
- In while loops, update loop variables before continue if needed.
- Keep loop body readable even with skip paths.
- Prefer simple conditions rather than deeply nested continue logic.
- In while loops, never skip increment/update paths accidentally.
Practice Task
- Change skip values from 3 and 6 to 2 and 5.
- Add one more skip rule for even values only.
- Build a while-loop version and ensure loop variable still updates.
- Print total executed cycles vs skipped cycles at loop end.
Try it now
Open simulator workspace and observe how continue skips selected loop cycles.