Lesson 24 - Using break Statement
Learn how break exits loops or switch blocks immediately when a stop condition is reached.
Progress indicator
Lesson 24 of 28
Learning Objectives
- Understand what break does in Arduino C/C++.
- Write correct break syntax in loops and switch blocks.
- Explain how break exits the current control block immediately.
- Use break safely in for and while loops.
- Understand how break behaves in nested loops and switch-case flow.
- Avoid common break mistakes in nested structures.
Concept Explanation
What is break
break immediately exits the current loop or switch block.
After break runs, program control continues with the next statement outside that block.
break Syntax
break;How break Works
- Code reaches a condition (for example
count == 5). break;executes.- The current loop/switch stops immediately.
- Execution continues after the block.
| Iteration | count | Action |
|---|---|---|
| 1-4 | 1..4 | Loop continues |
| 5 | 5 | break executes, loop exits |
| After exit | 5 | Code after loop runs |
break in for Loop
for (int i = 0; i < 10; i++) {
if (i == 3) {
break;
}
}break in while Loop
while (true) {
if (sensorValue > 2000) {
break;
}
}break in switch Statement
switch (mode) {
case 1:
Serial.println("Mode 1");
break;
default:
Serial.println("Default");
break;
}In switch, break prevents fall-through into the next case.
Without break, execution may continue to next case labels and produce unexpected output.
When to Use break
- Stop searching once target value is found
- Exit retry loop when operation succeeds
- Stop loop early for safety conditions
Example Code
This example exits a for loop when count reaches 5.
const int ledPin = 2;
int count = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
for (int i = 0; i < 10; i++) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
count = count + 1;
Serial.print("count = ");
Serial.println(count);
if (count == 5) {
Serial.println("break reached at count 5");
break;
}
}
delay(1000);
}Example Code Explanation
- The for loop is set to run up to 10 times.
- Each cycle blinks LED once and increments
count. - When
count == 5, condition becomes true. break;executes and exits the for loop immediately.- Program continues with code after the loop.
Real-life example
In a sensor scan loop, break can stop scanning early as soon as a fault value is found.
What Happens Inside
- Loop starts normally and repeats cycle-by-cycle.
- Condition check for break happens on every iteration.
- At match condition, break cuts off remaining iterations.
- Loop index is not completed to final bound after break.
- Execution jumps to the next statement outside the loop.
Nested loop note
break only exits the nearest loop. If you are inside nested loops, outer loops keep running.
Common Mistakes with break
- Using break outside loops or switch blocks.
- Forgetting break in switch, causing case fall-through.
- Placing break condition in wrong location inside loop.
- Assuming break exits all nested loops (it exits only current loop).
Best Practices for break
- Use clear break conditions with readable variable names.
- Log reason before break while debugging.
- In switch blocks, always include break unless fall-through is intentional.
- Document break use when loop exit is not obvious.
- Prefer one well-defined exit condition instead of many scattered break points.
Practice Task
- Change break condition from
count == 5tocount == 3. - Add Serial text right before break to explain why loop is stopping.
- Write one while-loop example that exits with break when button is pressed.
- Write a switch statement with 3 cases and verify each case uses break.
Try it now
Open simulator workspace and practice early loop exit patterns with break.