Windows脚本初探之PowerShell流程控制break
Windows PowerShell中break会直接退出一个while,do,for或者foreach循环,示例如下:示例1:
while(表达式1){ 代码块1
...
if(表达式2)break;
...
代码块2
}
如果表达式2成立,执行break直接跳出while循环;
break还有一个变种,它可以跳出多重套嵌的循环,示例如下:
示例2:
:mianloop while(true){ $text=readline()
for ($token =gettoken($text);-not isEmpty($token);$token=nexttoken()){
if($token -eq 'stop'){break mainloop}
process($token)
}
}
从上一个示例2中break通过“mianloop”来标记直接跳出了整个while循环,而不仅仅是第二层的for循环;
页:
[1]