break and continue
In this page:
break to Exit Early
break immediately terminates the nearest enclosing loop, and execution continues with the first statement after the loop's done. This is useful for stopping as soon as you've found what you were looking for, without processing the remaining items.
Example: break to Exit Early
#!/bin/bash
for num in 1 2 3 4 5; do
if [ "$num" -eq 3 ]; then
echo "Found 3, stopping the loop"
break
fi
echo "Checking $num"
done
Login to try C/C++/Java/PHP code in the editor
continue to Skip an Iteration
continue skips the rest of the current iteration's body and jumps straight to the next iteration (re-checking the loop condition or advancing to the next list item). This is handy for filtering out items you don't want to process further.
Example: continue to Skip an Iteration
#!/bin/bash
for num in 1 2 3 4 5; do
if (( num % 2 == 0 )); then
continue
fi
echo "Odd number: $num"
done
Login to try C/C++/Java/PHP code in the editor
break N and continue N for Nested Loops
When loops are nested, break N or continue N acts on the Nth loop counting outward from the innermost (so break 2 exits two levels of loops at once). Without a number, both default to affecting only the innermost loop.
Example: break N and continue N for Nested Loops
#!/bin/bash
for outer in 1 2 3; do
for inner in a b c; do
if [ "$inner" = b ]; then
continue 2
fi
echo "outer=$outer inner=$inner"
done
done
Login to try C/C++/Java/PHP code in the editor
Combining break and continue
break and continue can be used together in the same loop to handle different situations: skipping unwanted items with continue while still being ready to stop entirely with break once some overall goal is met. This keeps the loop body focused on one condition at a time instead of one giant nested if.
Example: Combining break and continue
#!/bin/bash
total=0
for num in 1 2 3 4 5 6 7 8; do
if (( num % 2 != 0 )); then
continue
fi
total=$((total + num))
if [ "$total" -ge 10 ]; then
echo "Reached total of $total, stopping"
break
fi
done
Login to try C/C++/Java/PHP code in the editor
- Confusing what
continueskips; it jumps straight to re-checking the loop's condition/next item, skipping only the *rest of the current iteration's body*, not the whole loop. - Forgetting that both
breakandcontinueaccept an optional numeric argument to affect an outer loop when loops are nested, e.g.break 2, and defaulting to assuming they only ever affect the innermost loop. - Using
break/continueoutside of any loop, which produces a shell warning/error since there is no loop context for them to act on.
breakimmediately exits the nearest enclosing loop, skipping any remaining iterations entirely.continueskips the rest of the current iteration's body and jumps to the next iteration's condition check.- Both accept an optional numeric argument,
break N/continue N, to affect the Nth enclosing loop when loops are nested (default is 1, the innermost). - Using
break/continuewell often makes loop logic clearer than deeply nestedifconditions inside the loop body.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: