Bird
0
0

You want to print only even numbers from 1 to 6 using a loop. Which script achieves this efficiently?

hard🚀 Application Q8 of 15
Bash Scripting - Loops
You want to print only even numbers from 1 to 6 using a loop. Which script achieves this efficiently?
Afor i in {1..6}; do if (( i % 2 == 0 )); then echo $i; fi; done
Bfor i in 2 4 8; do echo $i; done
Cfor i in {1..6}; do echo $i; done
Dwhile [ $i -le 6 ]; do echo $i; i=$((i+2)); done
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    Print only even numbers from 1 to 6 using a loop with condition.
  2. Step 2: Analyze options

    for i in {1..6}; do if (( i % 2 == 0 )); then echo $i; fi; done loops 1 to 6 and prints only if number is even using modulo check.
  3. Final Answer:

    for i in {1..6}; do if (( i % 2 == 0 )); then echo $i; fi; done -> Option A
  4. Quick Check:

    Loop with condition filters even numbers = B [OK]
Quick Trick: Use modulo (%) to check even numbers inside loops [OK]
Common Mistakes:
MISTAKES
  • Printing all numbers without filtering
  • Using incorrect loop ranges
  • Forgetting to increment in while loops

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes