break and continue in Bash Scripting - Time & Space Complexity
When using break and continue in bash loops, it is important to see how they affect the total steps the script runs.
We want to know how these commands change the time it takes as the input grows.
Analyze the time complexity of the following code snippet.
for i in {1..100}
do
if [ $i -eq 50 ]; then
break
fi
echo "Number $i"
done
This script prints numbers from 1 up to 49, then stops the loop early using break.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop iteration from 1 to 100.
- How many times: Loop runs until it hits 50, then stops early due to
break.
Because the loop stops early at 50, the number of steps does not grow with the full input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 (loop runs fully) |
| 100 | 50 (stops early at 50) |
| 1000 | 50 (still stops at 50) |
Pattern observation: The loop does not always run to the full input size because break stops it early.
Time Complexity: O(1)
This means the loop runs only up to a fixed number of steps (k), regardless of input size.
[X] Wrong: "The loop always runs n times, so time complexity is always O(n)."
[OK] Correct: Because break can stop the loop early, the actual steps can be fewer than n.
Understanding how break and continue affect loops shows you can reason about real script behavior, not just the code structure.
"What if we replaced break with continue? How would the time complexity change?"
Practice
break command do inside a bash loop?Solution
Step 1: Understand the role of
Thebreakin loopsbreakcommand immediately stops the loop and exits it, no matter where it is in the loop.Step 2: Compare with other commands
continueskips to the next iteration, butbreakstops the loop entirely.Final Answer:
Stops the loop immediately and exits it -> Option BQuick Check:
breakstops loop = D [OK]
- Confusing break with continue
- Thinking break skips iteration instead of stopping
- Assuming break restarts the loop
for loop?Solution
Step 1: Identify the command to skip iteration
In bash,continueis used to skip the current iteration and move to the next one.Step 2: Check other options
breakstops the loop,skipandnextare not valid bash commands.Final Answer:
continue -> Option CQuick Check:
Skip iteration = continue [OK]
- Using break instead of continue
- Using non-existent commands like skip or next
- Confusing syntax with other languages
for i in 1 2 3 4 5; do
if [ "$i" -eq 3 ]; then
break
fi
echo $i
doneSolution
Step 1: Analyze the loop and break condition
The loop runs from 1 to 5. Wheniequals 3, thebreakcommand stops the loop immediately.Step 2: Determine which values are printed
Before reaching 3, the script echoes 1 and 2. Wheniis 3, loop breaks, so 3 is not printed.Final Answer:
1 2 -> Option DQuick Check:
Break stops at 3, prints before = 1 2 [OK]
- Including the break value in output
- Confusing break with continue
- Assuming loop runs fully
for i in 1 2 3 4; do
if [ $i -eq 2 ]
continue
fi
echo $i
doneSolution
Step 1: Check if statement syntax
Theifstatement requires athenkeyword after the condition before commands.Step 2: Identify missing keyword
The script missesthenafter[ $i -eq 2 ], causing syntax error.Final Answer:
Missing then keyword after if condition -> Option AQuick Check:
If needs then before commands [OK]
- Omitting then after if
- Assuming continue needs semicolon
- Thinking continue can't be inside if
Solution
Step 1: Understand requirements
Skip printing 3 (use continue), stop loop if number > 4 (use break).Step 2: Analyze each option
for i in 1 2 3 4 5; do if [ "$i" -eq 3 ]; then continue; fi if [ "$i" -gt 4 ]; then break; fi echo $i done correctly uses continue to skip 3 and break to stop after 4. Others misuse break and continue or reverse conditions.Final Answer:
Option A code snippet -> Option AQuick Check:
continue skips 3, break stops after 4 [OK]
- Swapping break and continue logic
- Wrong condition order causing wrong output
- Not stopping loop after number > 4
Ready to prove it?
Pass the test and this pattern moves unknown
