0
0
Bash Scriptingscripting~10 mins

break and continue in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to stop the loop when the number is 5.

Bash Scripting
for i in {1..10}; do
  if [ "$i" -eq 5 ]; then
    [1]
  fi
  echo "$i"
done
Drag options to blanks, or click blank then click option'
Acontinue
Bbreak
Cexit
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'continue' instead of 'break' which skips the current iteration but does not stop the loop.
2fill in blank
medium

Complete the code to skip printing the number 3 but continue the loop.

Bash Scripting
for i in {1..5}; do
  if [ "$i" -eq 3 ]; then
    [1]
  fi
  echo "$i"
done
Drag options to blanks, or click blank then click option'
Acontinue
Bbreak
Cexit
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' which stops the loop instead of skipping one iteration.
3fill in blank
hard

Fix the error in the code to correctly skip even numbers and print only odd numbers.

Bash Scripting
for num in {1..6}; do
  if [ $((num % 2)) -eq 0 ]; then
    [1]
  fi
  echo "$num"
done
Drag options to blanks, or click blank then click option'
Acontinue
Bbreak
Cexit
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' which stops the loop instead of skipping even numbers.
4fill in blank
hard

Fill both blanks to stop the loop when number is 4 and skip printing number 2.

Bash Scripting
for n in {1..5}; do
  if [ "$n" -eq 2 ]; then
    [1]
  fi
  if [ "$n" -eq 4 ]; then
    [2]
  fi
  echo "$n"
done
Drag options to blanks, or click blank then click option'
Acontinue
Bbreak
Cexit
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' to skip number 2 which stops the loop too early.
Using 'continue' to stop the loop which only skips iterations.
5fill in blank
hard

Fill all three blanks to skip number 3, stop at number 6, and print the rest.

Bash Scripting
for val in {1..7}; do
  if [ "$val" -eq 3 ]; then
    [1]
  fi
  if [ "$val" -eq 6 ]; then
    [2]
  fi
  echo "$val"
done

# After loop ends
[3]
Drag options to blanks, or click blank then click option'
Acontinue
Bbreak
Cecho 'Loop finished'
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' to skip number 3 which stops the loop early.
Using 'exit' after the loop which ends the script abruptly.