Break in Loops | Exits the loop immediately, regardless of the loop condition. | <loop> { if (<condition>) { break } <code> } | powershell<br>for ($i = 1; $i -le 10; $i++) {<br> if ($i -eq 5) { break }<br> Write-Output $i<br>}<br> |
Continue in Loops | Skips the current iteration and proceeds with the next iteration of the loop. | <loop> { if (<condition>) { continue } <code> } | powershell<br>for ($i = 1; $i -le 10; $i++) {<br> if ($i -eq 5) { continue }<br> Write-Output $i<br>}<br> |
Labelled Loops | Allows the use of labels to control flow when breaking out of nested loops. | <label>: <loop> { <inner-loop> { if (<condition>) { break <label> } <code> } } | powershell<br>outer: for ($i = 1; $i -le 3; $i++) {<br> for ($j = 1; $j -le 3; $j++) {<br> if ($i -eq 2 -and $j -eq 2) {<br> break outer<br> }<br> Write-Output "i=$i, j=$j"<br> }<br>}<br> |
Looping with Pipeline (ForEach-Object) | Processes each item in the pipeline using the loop, ideal for handling large datasets. | “`<collection> | ForEach-Object { <code> }“` |
Nested Loops | Loops inside other loops, often used to handle multidimensional arrays or complex iterations. | <outer-loop> { <inner-loop> { <code> } } | powershell<br>for ($i = 1; $i -le 3; $i++) {<br> for ($j = 1; $j -le 3; $j++) {<br> Write-Output "i=$i, j=$j"<br> }<br>}<br> |
Do-While Loop with Condition at End | Executes the loop body at least once and then repeats until the condition is false. | do { <code> } while (<condition>) | powershell<br>$i = 1<br>do {<br> Write-Output $i<br> $i++<br>} while ($i -le 5)<br> |
Do-Until Loop with Condition at End | Executes the loop body at least once and then repeats until the condition is true. | do { <code> } until (<condition>) | powershell<br>$i = 1<br>do {<br> Write-Output $i<br> $i++<br>} until ($i -gt 5)<br> |
ForEach Loop with Multiple Actions | Iterates over a collection and performs multiple actions within the loop. | foreach (<item> in <collection>) { <action1>; <action2>; } | powershell<br>$array = @(1, 2, 3)<br>foreach ($item in $array) {<br> $squared = $item * $item<br> Write-Output "$item squared is $squared"<br>}<br> |
ForEach-Object with Conditional Logic | Applies conditional logic within a pipeline-based loop, filtering or transforming data as it passes through. | “`<collection> | ForEach-Object { if (<condition>) { <code> } }“` |
Using Array Indexes in Loops | Accesses and processes elements of an array by index within a loop. | for ($i = 0; $i -lt $array.Length; $i++) { <code> } | powershell<br>$array = @(1, 2, 3, 4, 5)<br>for ($i = 0; $i -lt $array.Length; $i++) {<br> Write-Output "Element $i: $($array[$i])"<br>}<br> |