Bird
0
0

What will be the output of this PowerShell script?

medium📝 Command Output Q4 of 15
PowerShell - Control Flow
What will be the output of this PowerShell script?
foreach ($i in 1..5) {
  if ($i -eq 3) { break }
  Write-Output $i
}
A1 2
BNo output
C3 4 5
D1 2 3 4 5
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop and break condition

    The loop runs from 1 to 5. When $i equals 3, the break statement stops the loop immediately.
  2. Step 2: Trace the output before break

    For $i = 1 and 2, the script outputs the value. When $i = 3, break stops the loop before output.
  3. Final Answer:

    1 2 -> Option A
  4. Quick Check:

    Break stops loop at 3, outputs before = D [OK]
Quick Trick: Break stops loop before output at condition [OK]
Common Mistakes:
  • Expecting output after break
  • Confusing break with continue
  • Ignoring break effect on loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes