Bird
0
0

Why does this while loop cause an infinite loop?

medium📝 Debug Q7 of 15
PowerShell - Control Flow
Why does this while loop cause an infinite loop?
$i = 1
while ($i -le 3) {
Write-Output $i
}
ALoop syntax is incorrect
BCondition uses wrong operator
CWrite-Output causes loop to restart
DVariable $i is never incremented inside the loop
Step-by-Step Solution
Solution:
  1. Step 1: Analyze loop body

    The loop prints $i but never changes its value.
  2. Step 2: Understand infinite loop cause

    Since $i stays 1, condition $i -le 3 is always true, causing infinite loop.
  3. Final Answer:

    Variable $i is never incremented inside the loop -> Option D
  4. Quick Check:

    Loop variable must change to avoid infinite loop [OK]
Quick Trick: Always update loop variable inside while loops [OK]
Common Mistakes:
  • Blaming condition operator
  • Thinking Write-Output affects loop control

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes