0
0
PowerShellscripting~5 mins

Break and continue in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the break statement do in a PowerShell loop?
The break statement immediately stops the entire loop and exits it, continuing with the code after the loop.
Click to reveal answer
beginner
What is the purpose of the continue statement in PowerShell loops?
The continue statement skips the rest of the current loop iteration and moves to the next iteration of the loop.
Click to reveal answer
beginner
How does break differ from continue in PowerShell loops?
break stops the whole loop immediately, while continue skips only the current iteration and continues looping.
Click to reveal answer
beginner
Example: What will this PowerShell code output?<br>
for ($i=1; $i -le 5; $i++) { if ($i -eq 3) { break } Write-Output $i }
It will output:<br>1<br>2<br>Because when $i equals 3, break stops the loop immediately.
Click to reveal answer
beginner
Example: What will this PowerShell code output?<br>
for ($i=1; $i -le 5; $i++) { if ($i -eq 3) { continue } Write-Output $i }
It will output:<br>1<br>2<br>4<br>5<br>Because when $i equals 3, continue skips that iteration and moves to the next one.
Click to reveal answer
What happens when break is used inside a PowerShell loop?
ANothing happens, loop runs normally.
BThe current iteration is skipped, loop continues.
CThe loop restarts from the beginning.
DThe loop stops immediately and exits.
What does continue do in a PowerShell loop?
AStops the loop completely.
BSkips the rest of the current iteration and continues with the next.
CRestarts the loop from the first iteration.
DEnds the script.
Which statement would you use to skip printing number 3 but continue the loop in PowerShell?
Acontinue
Bexit
Cbreak
Dstop
If you want to stop a loop as soon as a condition is met, which statement is best?
Abreak
Bcontinue
Creturn
Dexit
What will this code output?<br>
for ($i=1; $i -le 4; $i++) { if ($i -eq 2) { continue } Write-Output $i }
A1 2 3 4
B2 3 4
C1 3 4
D1 2 4
Explain in your own words how break and continue control the flow inside PowerShell loops.
Think about what happens when you want to stop everything vs just skip one step.
You got /3 concepts.
    Write a simple PowerShell loop that prints numbers 1 to 5 but skips number 3 using continue.
    Use an if statement inside the loop to check for 3 and skip it.
    You got /4 concepts.