Bird
0
0

Identify the error in this PowerShell while loop code:

medium📝 Debug Q14 of 15
PowerShell - Control Flow
Identify the error in this PowerShell while loop code:
 $i = 5
while ($i -lt 5)
{
Write-Output $i
$i++
}
AThe loop condition is never true, so loop body never runs.
BThe increment operator <code>$i++</code> is invalid in PowerShell.
CMissing parentheses around the loop body.
DThe variable <code>$i</code> is not initialized.
Step-by-Step Solution
Solution:
  1. Step 1: Check initial condition

    The variable $i is initialized to 5. The loop condition is $i -lt 5 (less than 5). Since 5 is not less than 5, the condition is false initially.
  2. Step 2: Understand loop execution

    Because the condition is false at the start, the while loop body never runs, so no output is produced.
  3. Final Answer:

    The loop condition is never true, so loop body never runs. -> Option A
  4. Quick Check:

    Initial condition false means no loop execution [OK]
Quick Trick: Check if loop condition is true initially to run loop [OK]
Common Mistakes:
  • Assuming loop runs at least once
  • Thinking $i++ is invalid in PowerShell
  • Confusing syntax for loop body braces
  • Ignoring initial variable value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes