Bird
0
0

How would you write a C-style for loop in bash to print even numbers from 2 to 10 inclusive?

hard🚀 Application Q8 of 15
Bash Scripting - Loops
How would you write a C-style for loop in bash to print even numbers from 2 to 10 inclusive?
Afor ((i=0; i<=10; i+=2)); do echo $i; done
Bfor ((i=1; i<10; i++)); do if (( i % 2 == 0 )); then echo $i; fi; done
Cfor ((i=2; i<10; i+=2)); do echo $i; done
Dfor ((i=2; i<=10; i+=2)); do echo $i; done
Step-by-Step Solution
Solution:
  1. Step 1: Identify loop start, end, and step

    Start at 2, end at 10 inclusive, step by 2 to get even numbers.
  2. Step 2: Check options for correct range and increment

    for ((i=2; i<=10; i+=2)); do echo $i; done matches start=2, end=10, increment=2 correctly.
  3. Final Answer:

    for ((i=2; i<=10; i+=2)); do echo $i; done -> Option D
  4. Quick Check:

    Even numbers loop = for ((i=2; i<=10; i+=2)); do echo $i; done [OK]
Quick Trick: Use i+=2 to step through even numbers [OK]
Common Mistakes:
MISTAKES
  • Using wrong start or end values
  • Missing inclusive condition
  • Using modulo unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes