Bird
0
0

You want to print only even numbers from 2 to 10 using a for loop in PowerShell. Which code snippet achieves this?

hard📝 Application Q15 of 15
PowerShell - Control Flow

You want to print only even numbers from 2 to 10 using a for loop in PowerShell. Which code snippet achieves this?

Afor ($i=2; $i -le 10; $i+=2) { Write-Output $i }
Bfor ($i=1; $i -lt 10; $i++) { if ($i % 2 -eq 0) { Write-Output $i } }
Cfor ($i=2; $i -lt 10; $i++) { Write-Output $i }
Dfor ($i=0; $i -le 10; $i+=3) { Write-Output $i }
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want to print even numbers from 2 to 10 inclusive.
  2. Step 2: Analyze each option

    for ($i=1; $i -lt 10; $i++) { if ($i % 2 -eq 0) { Write-Output $i } } goes to i<10 so prints 2,4,6,8 (misses 10). for ($i=2; $i -le 10; $i+=2) { Write-Output $i } starts at 2, increments by 2 up to 10: 2,4,6,8,10. for ($i=2; $i -lt 10; $i++) { Write-Output $i } prints 2-9 all numbers. for ($i=0; $i -le 10; $i+=3) { Write-Output $i } wrong start/step.
  3. Final Answer:

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

    Increment by 2 from 2 to 10 = even numbers [OK]
Quick Trick: Increment by 2 starting at 2 for even numbers [OK]
Common Mistakes:
  • Using wrong increment step
  • Starting from 1 instead of 2
  • Not filtering for even numbers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes