Challenge - 5 Problems
Break & Continue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of break in nested loops
What is the output of this Bash script?
Bash Scripting
for i in 1 2 3; do for j in a b c; do if [[ $j == b ]]; then break fi echo "$i$j" done done
Attempts:
2 left
💡 Hint
Remember that break exits the innermost loop immediately.
✗ Incorrect
The break statement stops the inner loop when j equals 'b'. So only 'a' is printed for each i.
💻 Command Output
intermediate2:00remaining
Effect of continue in a loop
What output does this Bash script produce?
Bash Scripting
for i in 1 2 3 4; do if [[ $i -eq 3 ]]; then continue fi echo $i done
Attempts:
2 left
💡 Hint
Continue skips the current iteration and moves to the next.
✗ Incorrect
When i equals 3, continue skips the echo command, so 3 is not printed.
📝 Syntax
advanced2:00remaining
Identify the syntax error with break usage
Which option contains a syntax error in using break inside a Bash script?
Bash Scripting
for i in 1 2 3; do if [[ $i -eq 2 ]]; then break 2 fi echo $i done
Attempts:
2 left
💡 Hint
Check if break accepts negative numbers as arguments.
✗ Incorrect
break with a negative argument is invalid syntax in Bash and causes an error.
🚀 Application
advanced2:00remaining
Using continue to skip even numbers
Which script correctly prints only odd numbers from 1 to 5 using continue?
Attempts:
2 left
💡 Hint
Continue skips printing even numbers; only odd numbers get echoed.
✗ Incorrect
Option D skips even numbers and prints odd numbers 1, 3, 5 correctly.
🧠 Conceptual
expert3:00remaining
Behavior of break in multiple nested loops
Consider this Bash script with nested loops. What is the output?
Bash Scripting
for i in 1 2; do for j in a b; do for k in x y; do if [[ $k == y ]]; then break 2 fi echo "$i$j$k" done done done
Attempts:
2 left
💡 Hint
break 2 exits two loops up from the current one.
✗ Incorrect
When k equals 'y', break 2 exits the inner two loops, skipping the rest of j and k loops for current i.