Bird
0
0

What will be the output of this PowerShell script?

medium📝 Command Output Q5 of 15
PowerShell - Control Flow
What will be the output of this PowerShell script?
$i = 4
while ($i -ge 0) {
Write-Output $i
$i -= 3
}
A4 1
B4 1 -2
C4 3 0
D4 1 -1
Step-by-Step Solution
Solution:
  1. Step 1: Initial value

    $i starts at 4.
  2. Step 2: Loop iterations

    First iteration: output 4, then $i = 4 - 3 = 1.
    Second iteration: output 1, then $i = 1 - 3 = -2.
    Third iteration: output -2, then $i = -2 - 3 = -5.
  3. Step 3: Loop condition check

    After third output, $i is -5 which is not greater or equal to 0, so loop stops.
  4. Final Answer:

    4 1 -2 -> Option B
  5. Quick Check:

    Loop runs while $i >= 0 [OK]
Quick Trick: Check condition after each decrement [OK]
Common Mistakes:
  • Stopping loop before last valid output
  • Misunderstanding -ge operator
  • Forgetting to decrement inside loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes