Bird
0
0

How can you modify this script to skip printing even numbers?

hard🚀 Application Q9 of 15
Bash Scripting - Loops
How can you modify this script to skip printing even numbers?
num=1
while [ $num -le 5 ]; do
  echo $num
  ((num++))
done
AAdd 'if (( num % 2 == 1 )); then echo $num; fi' inside the loop
BChange condition to '[ $num -eq 1 ]'
CRemove the increment statement
DUse 'continue' before echo
Step-by-Step Solution
Solution:
  1. Step 1: Identify how to skip even numbers

    Use modulo operator to check if number is odd.
  2. Step 2: Add conditional echo

    Print number only if it is odd using if statement inside loop.
  3. Final Answer:

    Add 'if (( num % 2 == 1 )); then echo $num; fi' inside the loop -> Option A
  4. Quick Check:

    Use modulo and if to skip evens [OK]
Quick Trick: Use modulo (%) to test odd/even inside while loop [OK]
Common Mistakes:
MISTAKES
  • Changing loop condition incorrectly
  • Removing increment causing infinite loop
  • Misusing continue statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes