Challenge - 5 Problems
PowerShell Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell script using break?
Consider the following script. What will it output?
PowerShell
for ($i = 1; $i -le 5; $i++) { if ($i -eq 3) { break } Write-Output $i }
Attempts:
2 left
💡 Hint
Remember, 'break' stops the entire loop immediately.
✗ Incorrect
The loop counts from 1 to 5. When $i equals 3, 'break' stops the loop, so only 1 and 2 are output.
💻 Command Output
intermediate2:00remaining
What does this PowerShell script print using continue?
Look at this script. What will it print?
PowerShell
for ($i = 1; $i -le 5; $i++) { if ($i -eq 3) { continue } Write-Output $i }
Attempts:
2 left
💡 Hint
'continue' skips the current loop iteration but keeps looping.
✗ Incorrect
When $i is 3, 'continue' skips the Write-Output line, so 3 is not printed. All other numbers print.
🔧 Debug
advanced2:00remaining
Why does this PowerShell script output only 1?
This script is supposed to print numbers 1 to 5 but only prints 1. Why?
PowerShell
for ($i = 1; $i -le 5; $i++) { if ($i -eq 2) { break } Write-Output $i continue }
Attempts:
2 left
💡 Hint
'break' stops the loop completely when condition is met.
✗ Incorrect
When $i equals 2, 'break' stops the loop, so only the first iteration prints 1.
💻 Command Output
advanced2:00remaining
What is the output of this nested loop with break and continue?
Analyze this script and choose the correct output.
PowerShell
for ($i = 1; $i -le 3; $i++) { for ($j = 1; $j -le 3; $j++) { if ($j -eq 2) { continue } if ($i -eq 2) { break } Write-Output "$i,$j" } }
Attempts:
2 left
💡 Hint
'continue' skips inner loop iteration; 'break' stops inner loop when $i=2.
✗ Incorrect
For $i=1, inner loop skips $j=2 and prints 1,1 and 1,3. For $i=2, inner loop breaks immediately, so no output. For $i=3, same as $i=1.
🧠 Conceptual
expert2:00remaining
Which statement about break and continue in PowerShell is TRUE?
Choose the correct statement about how 'break' and 'continue' work in PowerShell loops.
Attempts:
2 left
💡 Hint
Think about what happens when you want to stop looping versus skipping one step.
✗ Incorrect
'break' stops the whole loop immediately. 'continue' skips the rest of the current loop step and moves to the next.