Bird
0
0

Which PowerShell for loop correctly prints all odd numbers from 1 to 9?

hard📝 Application Q8 of 15
PowerShell - Control Flow

Which PowerShell for loop correctly prints all odd numbers from 1 to 9?

Afor ($i=1; $i -le 9; $i+=2) { Write-Output $i }
Bfor ($i=2; $i -le 10; $i+=2) { Write-Output $i }
Cfor ($i=1; $i -lt 10; $i++) { if ($i % 2 -eq 0) { Write-Output $i } }
Dfor ($i=0; $i -le 9; $i+=3) { Write-Output $i }
Step-by-Step Solution
Solution:
  1. Step 1: Identify odd numbers range

    Odd numbers from 1 to 9 are 1, 3, 5, 7, 9.
  2. Step 2: Check loop increments

    for ($i=1; $i -le 9; $i+=2) { Write-Output $i } starts at 1 and increments by 2, printing only odd numbers.
  3. Step 3: Verify other options

    for ($i=2; $i -le 10; $i+=2) { Write-Output $i } prints even numbers; for ($i=1; $i -lt 10; $i++) { if ($i % 2 -eq 0) { Write-Output $i } } prints even numbers due to condition; for ($i=0; $i -le 9; $i+=3) { Write-Output $i } increments by 3 starting at 0, printing 0,3,6,9.
  4. Final Answer:

    for ($i=1; $i -le 9; $i+=2) { Write-Output $i } -> Option A
  5. Quick Check:

    Start at 1, increment by 2 for odds [OK]
Quick Trick: Increment by 2 starting at 1 for odd numbers [OK]
Common Mistakes:
  • Starting at 2 for odd numbers
  • Using wrong increment values
  • Incorrect conditional checks inside loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes