0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.