Challenge - 5 Problems
Loop Mastery in PowerShell
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of a While loop counting down
What is the output of this PowerShell script?
PowerShell
i=3 while ($i -gt 0) { Write-Output $i $i-- }
Attempts:
2 left
💡 Hint
Remember the loop runs while $i is greater than zero and decreases $i each time.
✗ Incorrect
The loop starts at 3 and prints the value before decreasing it. It stops when $i is no longer greater than 0, so it prints 3, 2, and 1.
💻 Command Output
intermediate2:00remaining
Output of a Do-While loop with a condition
What will this PowerShell script output?
PowerShell
i=1 do { Write-Output $i $i += 2 } while ($i -lt 6)
Attempts:
2 left
💡 Hint
The loop adds 2 to $i each time and continues while $i is less than 6.
✗ Incorrect
The loop starts at 1, prints it, then adds 2. It prints 1, 3, and 5. When $i becomes 7, the condition fails and the loop stops.
🔧 Debug
advanced2:00remaining
Identify the error in this While loop
What error will this PowerShell script produce?
PowerShell
i=0 while ($i < 3) { Write-Output $i i++ }
Attempts:
2 left
💡 Hint
Check how variables are referenced and incremented in PowerShell.
✗ Incorrect
In PowerShell, variables must have a '$' prefix. The increment 'i++' misses the '$', causing a syntax error.
🚀 Application
advanced2:00remaining
Count how many times a Do-While loop runs
How many times will this PowerShell Do-While loop execute?
PowerShell
$count = 0 do { $count++ } while ($count -lt 5)
Attempts:
2 left
💡 Hint
The loop runs while $count is less than 5 and increments $count each time.
✗ Incorrect
The loop starts at 0, increments $count each time, and stops after $count reaches 5, so it runs 5 times.
🧠 Conceptual
expert2:00remaining
Difference between While and Do-While loops in PowerShell
Which statement correctly describes the difference between While and Do-While loops in PowerShell?
Attempts:
2 left
💡 Hint
Think about when the condition is evaluated in each loop type.
✗ Incorrect
While loops test the condition first and may not run if false initially. Do-While loops always run the body once before testing the condition.