Bird
0
0

What will this PowerShell script output?

medium📝 Command Output Q4 of 15
PowerShell - Control Flow
What will this PowerShell script output?
for ($i=1; $i -le 4; $i++) { if ($i -eq 3) { break } Write-Output $i }
A1 2
B1 2 3
C3 4
D1 2 3 4
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the loop

    The loop runs from 1 to 4.
  2. Step 2: Check the break condition

    When $i equals 3, the break statement exits the loop immediately.
  3. Step 3: Determine output

    Only values 1 and 2 are output before the loop breaks.
  4. Final Answer:

    1 2 -> Option A
  5. Quick Check:

    Break exits loop before outputting 3 [OK]
Quick Trick: Break stops loop before outputting current value [OK]
Common Mistakes:
  • Assuming break outputs the current value before exiting
  • Confusing break with continue
  • Ignoring loop exit on break

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes