Bird
Raised Fist0
Bash Scriptingscripting~20 mins

break and continue in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Break & Continue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
A
1a
1b
2a
2b
3a
3b
B
1a
2a
3a
C
1a
1b
1c
2a
2b
2c
3a
3b
3c
D
1a
2a
3a
1b
2b
3b
Attempts:
2 left
💡 Hint
Remember that break exits the innermost loop immediately.
💻 Command Output
intermediate
2: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
A
1
2
4
B
1
2
3
4
C
2
3
4
D
1
2
3
Attempts:
2 left
💡 Hint
Continue skips the current iteration and moves to the next.
📝 Syntax
advanced
2: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
Abreak
Bbreak 0
Cbreak -1
Dbreak 2
Attempts:
2 left
💡 Hint
Check if break accepts negative numbers as arguments.
🚀 Application
advanced
2:00remaining
Using continue to skip even numbers
Which script correctly prints only odd numbers from 1 to 5 using continue?
A
for i in {1..5}; do
  if [ $((i % 2)) -eq 1 ]; then
    continue
  fi
  echo $i
done
B
for i in {1..5}; do
  if (( i % 2 != 0 )); then
    continue
  fi
  echo $i
done
C
for i in {1..5}; do
  if [ $((i % 2)) -eq 0 ]; then
    echo $i
    continue
  fi
done
D
for i in {1..5}; do
  if (( i % 2 == 0 )); then
    continue
  fi
  echo $i
done
Attempts:
2 left
💡 Hint
Continue skips printing even numbers; only odd numbers get echoed.
🧠 Conceptual
expert
3: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
A
1ax
2ax
B
1ax
1ay
1bx
1by
C
1ax
1bx
2ax
2bx
D
1ax
1ay
1bx
1by
2ax
2ay
2bx
2by
Attempts:
2 left
💡 Hint
break 2 exits two loops up from the current one.

Practice

(1/5)
1. What does the break command do inside a bash loop?
easy
A. Pauses the loop until user input
B. Stops the loop immediately and exits it
C. Restarts the loop from the beginning
D. Skips the current iteration and continues with the next

Solution

  1. Step 1: Understand the role of break in loops

    The break command immediately stops the loop and exits it, no matter where it is in the loop.
  2. Step 2: Compare with other commands

    continue skips to the next iteration, but break stops the loop entirely.
  3. Final Answer:

    Stops the loop immediately and exits it -> Option B
  4. Quick Check:

    break stops loop = D [OK]
Hint: Break means stop loop now, continue means skip one round [OK]
Common Mistakes:
  • Confusing break with continue
  • Thinking break skips iteration instead of stopping
  • Assuming break restarts the loop
2. Which of the following is the correct syntax to skip the current iteration in a bash for loop?
easy
A. break
B. skip
C. continue
D. next

Solution

  1. Step 1: Identify the command to skip iteration

    In bash, continue is used to skip the current iteration and move to the next one.
  2. Step 2: Check other options

    break stops the loop, skip and next are not valid bash commands.
  3. Final Answer:

    continue -> Option C
  4. Quick Check:

    Skip iteration = continue [OK]
Hint: Use continue to skip current loop round in bash [OK]
Common Mistakes:
  • Using break instead of continue
  • Using non-existent commands like skip or next
  • Confusing syntax with other languages
3. What will be the output of this bash script?
for i in 1 2 3 4 5; do
  if [ "$i" -eq 3 ]; then
    break
  fi
  echo $i
done
medium
A. 1 2 3 4 5
B. 3 4 5
C. 1 2 3
D. 1 2

Solution

  1. Step 1: Analyze the loop and break condition

    The loop runs from 1 to 5. When i equals 3, the break command stops the loop immediately.
  2. Step 2: Determine which values are printed

    Before reaching 3, the script echoes 1 and 2. When i is 3, loop breaks, so 3 is not printed.
  3. Final Answer:

    1 2 -> Option D
  4. Quick Check:

    Break stops at 3, prints before = 1 2 [OK]
Hint: Break stops loop before printing current value [OK]
Common Mistakes:
  • Including the break value in output
  • Confusing break with continue
  • Assuming loop runs fully
4. Identify the error in this bash script snippet:
for i in 1 2 3 4; do
  if [ $i -eq 2 ]
    continue
  fi
  echo $i
done
medium
A. Missing then keyword after if condition
B. Missing semicolon before continue
C. continue cannot be used inside if
D. echo command is misplaced

Solution

  1. Step 1: Check if statement syntax

    The if statement requires a then keyword after the condition before commands.
  2. Step 2: Identify missing keyword

    The script misses then after [ $i -eq 2 ], causing syntax error.
  3. Final Answer:

    Missing then keyword after if condition -> Option A
  4. Quick Check:

    If needs then before commands [OK]
Hint: Always put then after if condition in bash [OK]
Common Mistakes:
  • Omitting then after if
  • Assuming continue needs semicolon
  • Thinking continue can't be inside if
5. You want to print numbers 1 to 5 but skip 3 and stop the loop if number is greater than 4. Which code snippet achieves this?
hard
A. 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
B. for i in 1 2 3 4 5; do if [ "$i" -gt 4 ]; then continue; fi if [ "$i" -eq 3 ]; then break; fi echo $i done
C. for i in 1 2 3 4 5; do if [ "$i" -eq 3 ]; then break; fi if [ "$i" -gt 4 ]; then continue; fi echo $i done
D. for i in 1 2 3 4 5; do if [ "$i" -eq 3 ]; then continue; fi if [ "$i" -gt 4 ]; then continue; fi echo $i done

Solution

  1. Step 1: Understand requirements

    Skip printing 3 (use continue), stop loop if number > 4 (use break).
  2. 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.
  3. Final Answer:

    Option A code snippet -> Option A
  4. Quick Check:

    continue skips 3, break stops after 4 [OK]
Hint: Use continue to skip, break to stop loop [OK]
Common Mistakes:
  • Swapping break and continue logic
  • Wrong condition order causing wrong output
  • Not stopping loop after number > 4
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard