Bird
0
0

What will this PowerShell code output?

medium📝 Predict Output Q13 of 15
PowerShell - Control Flow
What will this PowerShell code output?
for ($i = 1; $i -le 3; $i++) { if ($i -eq 2) { continue } Write-Output $i }
A1
B1 2 3
C2 3
D1 3
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop and continue

    The loop runs $i from 1 to 3. When $i equals 2, 'continue' skips the rest of that iteration.
  2. Step 2: Trace output values

    Output occurs only when $i is not 2, so outputs are 1 and 3.
  3. Final Answer:

    1 3 -> Option D
  4. Quick Check:

    continue skips 2, outputs 1 and 3 [OK]
Quick Trick: continue skips current loop step, so that value is not output [OK]
Common Mistakes:
  • Thinking continue outputs the skipped value
  • Confusing continue with break
  • Ignoring the if condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes