Bird
0
0

Find the error in this PowerShell script:

medium📝 Debug Q7 of 15
PowerShell - Control Flow
Find the error in this PowerShell script:
for ($i=1; $i -le 5; $i++) {
  if ($i -eq 3) break
  else continue
  Write-Output $i
}
AThe for loop syntax is invalid.
BThe break statement is incorrectly placed.
CThe Write-Output statement is unreachable due to continue.
DThe else keyword cannot be used with continue.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the flow of the loop

    If $i equals 3, break exits the loop. Otherwise, continue skips the rest of the iteration.
  2. Step 2: Check the position of Write-Output

    Write-Output is after continue, so it never runs because continue skips to next iteration immediately.
  3. Final Answer:

    The Write-Output statement is unreachable due to continue. -> Option C
  4. Quick Check:

    Continue skips code after it = A [OK]
Quick Trick: Code after continue in loop is skipped [OK]
Common Mistakes:
  • Expecting Write-Output to run after continue
  • Misunderstanding break placement
  • Thinking else is invalid with continue

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes