我有一个foreach循环,如下例所示:
foreach ($a in @(1,2,3,4))
{ switch ($a)
{ 1 { $x = "value a"
Write-Host "Not yet supported."
continue
break
}
2 { $x = "value b"
Write-Host "Not yet supported."
continue
break
}
3 { $x = "value c"
break
}
4 { $x = "value d"
break
}
}
Write-Host $a
}
预期产出:
Not yet supported.
Not yet supported.
3
4
实际输出:
Not yet supported.
1
Not yet supported.
2
3
4
所以问题是,为什么继续不能在switch语句中工作?我可以将它重写为if语句,但这不在我的愿望清单上......
P.S。如果breaks
按预期工作,则前两个continue
不会产生任何影响。这是有意的,因为不支持的消息将在接下来的几周内被删除:)
修改
根据这篇文章:is there any difference in using a break or a continue statement within a switch statement?,switch可以处理非标量值......