0
0
PowerShellscripting~10 mins

Break and continue in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Break and continue
Start Loop
Check Condition
Inside Loop Body
If 'continue' condition?
YesSkip rest, next iteration
If 'break' condition?
YesExit Loop
Next Iteration
Back to Check Condition
End Loop
Back to Check Condition
The loop starts and checks the condition. Inside the loop, if 'continue' is met, it skips to the next iteration. If 'break' is met, it exits the loop immediately.
Execution Sample
PowerShell
for ($i = 1; $i -le 5; $i++) {
  if ($i -eq 3) { continue }
  if ($i -eq 4) { break }
  Write-Output $i
}
This loop prints numbers 1 to 5 but skips 3 and stops completely at 4.
Execution Table
IterationiCondition i -le 5Check i -eq 3 (continue?)Check i -eq 4 (break?)ActionOutput
11TrueFalseFalsePrint 11
22TrueFalseFalsePrint 22
33TrueTrueFalseContinue (skip print)
44TrueFalseTrueBreak (exit loop)
5N/AN/AN/AN/ALoop ended
💡 Loop stops at iteration 4 because break condition is met (i equals 4).
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
i1234N/AN/A
Key Moments - 2 Insights
Why does the number 3 not get printed even though the loop runs at i=3?
At iteration 3 in the execution_table, the 'continue' condition is true, so the loop skips the print action and moves to the next iteration.
Why does the loop stop completely at i=4 and not continue to 5?
At iteration 4, the 'break' condition is true, so the loop exits immediately, as shown in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at iteration 3?
ANo output
B4
C3
DError
💡 Hint
Check the 'Action' and 'Output' columns for iteration 3 in the execution_table.
At which iteration does the loop stop running?
A2
B3
C4
D5
💡 Hint
Look at the 'Action' column where 'Break (exit loop)' happens in the execution_table.
If the 'continue' line was removed, what would be the output at iteration 3?
ALoop would break at 3
B3 would be printed
CNo output at 3
DError occurs
💡 Hint
Without 'continue', the print action at iteration 3 would run as shown in the execution_table for other iterations.
Concept Snapshot
PowerShell loops can use 'break' to stop the loop immediately.
Use 'continue' to skip the rest of the current loop iteration.
'continue' jumps to the next iteration.
'break' exits the loop completely.
Check conditions inside the loop to control flow.
Useful to skip or stop loops based on conditions.
Full Transcript
This example shows a PowerShell for loop from 1 to 5. At each iteration, it checks if the current number is 3 or 4. If it is 3, the loop uses 'continue' to skip printing that number and moves to the next iteration. If it is 4, the loop uses 'break' to stop running completely. The output prints 1 and 2 only. The variable 'i' changes from 1 to 4, then the loop ends. This teaches how 'break' and 'continue' control loop flow by skipping or stopping iterations.