The Continue statement is used in PowerShell to return the flow of the program to the top of an innermost loop. This statement is controlled by the for, Foreach and while loop.
When this statement is executed in a loop, the execution of code inside that loop following the continue statement will be skipped, and the next iteration of a loop will begin. It is generally used for a condition so that a user can skip some code for a specific condition.
Examples
Example 1: The following example displays the number from 1 to 10 but not 5 using continue statement in while loop:
PS C:\> $a = 1
PS C:\> while ($a -le 10)
>> {
>> if ($a -eq 5)
>> { $a += 1
>> continue
>> }
>> echo $a
>> $a += 1
>> }
Output:1 2 3 4 6 7 8 9 10
The Value 5 is missing in the output because when the value of variable $a is 5 if condition is true and the continue statement encountered after the increment of the variable, which makes the control to jump at the beginning of the while loop for next iteration, skipping the statements for current iteration so that’s why the echo command will not execute for the current iteration.
Leave a Reply