PowerShell Continue statement example
This article describes how to use the PowerShell Continue statement. This article provides the sample code directly. For more information, see
When you use the "Continue" statement in a loop, you can skip the current iteration of the loop and Continue to execute the next iteration. If the "Break" statement is used, all the remaining iteration cycles are skipped.
This raises a problem: in a multi-layer nested loop, which loop will be affected. By default, "Continue" only affects the inner loop, but you can also use "Continue" and "Break" to point to a tag of the outer loop.
?
1 2 3 4 5 6 7 8 9 10 11 |
: Outer Foreach ($ element in (1 .. 10 )) { For ($ x = 1000; $ x-lt 1500; $ x + = 100) { "Frequency $ x Hz" [Console]: Beep ($ x, 500) Continue outer Write-Host 'you and I will never meet again unless you change the code' } } |
Because the continue statement in the above example is adjusted to the outer loop, we will see 10 output of HZ. If the continue statement is deleted, the number of loops increases significantly, the write-host statement will not be skipped.