0
0
PowerShellscripting~20 mins

Break and continue in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
}
A
1
2
3
4
5
B
3
4
5
C
1
2
D
1
2
3
Attempts:
2 left
💡 Hint
Remember, 'break' stops the entire loop immediately.
💻 Command Output
intermediate
2: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
}
A
1
2
4
5
B
1
2
3
4
5
C3
D
1
2
3
4
Attempts:
2 left
💡 Hint
'continue' skips the current loop iteration but keeps looping.
🔧 Debug
advanced
2: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
}
ABecause 'continue' after Write-Output skips printing 2.
BBecause 'break' stops the loop when $i is 2, so only 1 prints.
CBecause the loop condition is wrong and stops early.
DBecause Write-Output is inside the if block and only runs once.
Attempts:
2 left
💡 Hint
'break' stops the loop completely when condition is met.
💻 Command Output
advanced
2: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"
  }
}
A
1,1
1,2
1,3
3,1
3,3
B
1,1
1,3
2,1
2,3
3,1
3,3
C
1,1
1,3
2,1
3,1
3,3
D
1,1
1,3
3,1
3,3
Attempts:
2 left
💡 Hint
'continue' skips inner loop iteration; 'break' stops inner loop when $i=2.
🧠 Conceptual
expert
2:00remaining
Which statement about break and continue in PowerShell is TRUE?
Choose the correct statement about how 'break' and 'continue' work in PowerShell loops.
A'break' exits the entire loop immediately; 'continue' skips to the next iteration of the loop.
B'break' can only be used in switch statements; 'continue' only in loops.
C'break' and 'continue' both skip the current iteration but differ in syntax.
D'break' exits only the current iteration; 'continue' exits the entire loop.
Attempts:
2 left
💡 Hint
Think about what happens when you want to stop looping versus skipping one step.