当Label带有break语句时,PowerShell会退出标签而不是退出当前循环。
$i = 1 while ($i -lt 10) { Write-Output "i = $i" if($i -eq 5){ Write-Output "Break statement executed" Break :mylable } $i++ } Write-Output "Entering to another loop" $j = 1 :mylable while($j -lt 3){ Write-Output "j = $j" $j++ }
输出结果
i = 1 i = 2 i = 3 i = 4 i = 5 Break statement executed Entering to another loop j = 1 j = 2
如您在上面的示例中看到的那样,当执行5的值时,还将执行包含标签(可修改)的块,并且执行将移至另一个循环。