← Back to Bash Course | Chapter 4: Loops | Lesson 10 of 14

break and continue

break and continue give you fine-grained control inside a loop: break stops the whole loop immediately, while continue just skips ahead to the next iteration.

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

bash
#!/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

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

bash
#!/bin/bash
for num in 1 2 3 4 5; do
    if (( num % 2 == 0 )); then
        continue
    fi
    echo "Odd number: $num"
done

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

bash
#!/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

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

bash
#!/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
Common Mistakes
  1. Confusing what continue skips; 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.
  2. Forgetting that both break and continue accept 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.
  3. Using break/continue outside of any loop, which produces a shell warning/error since there is no loop context for them to act on.
Chapter Summary
  • break immediately exits the nearest enclosing loop, skipping any remaining iterations entirely.
  • continue skips 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/continue well often makes loop logic clearer than deeply nested if conditions inside the loop body.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.