开关内部中断无法终止 FOR 循环
我有一个代码片段:
int n = 0;
for (int i = 0; i < 50;i++)
{
n = checkStatus();
switch (n)
{
case 1:
break;
break;//This is unreachable and so i cannot Terminate the For Loop within the SWITCH
}
}
如评论中所述,只有当我声明一个布尔值并在 Switch 测试结束时,我才能直接从 Switch 终止 For 循环
if(LoopShouldTerminate)
break;
PS:或者也许我很困惑!
[发布] 我收到消息,问题已解决,但我想假设在 for 循环中使用 Switch 不是一个好主意,因为我从很多开发人员那里听说,当我得到想要的结果,所以使用 switch 需要额外的布尔值或将 Int i 值直接推到 50,但是如果我们使用 while 循环会发生什么?
I have a code snippet :
int n = 0;
for (int i = 0; i < 50;i++)
{
n = checkStatus();
switch (n)
{
case 1:
break;
break;//This is unreachable and so i cannot Terminate the For Loop within the SWITCH
}
}
As described in a comment I cannot terminate the For Loop directly from the Switch, only if I declare a boolean and at the End of Switch test
if(LoopShouldTerminate)
break;
PS : or maybe I'm very confused!
[POST]
I got the message ,and the problem is Solved ,but i would like to asume that using Switch within a for loop isn't a god idea ,because i heard from lot of developer's i should break from loop in the moment when i get the desired result ,so using switch need's extra boolean or push the Int i value to 50 direclty , but what would happen if we're using while loop ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
解决方案 1:将循环和 switch 移至不同的方法:
...
解决方案 2:采用上述方法来消除使用急切扩展方法的循环:
解决方案 3:采用上述方法来消除循环和辅助方法:
Solution 1: Move the loop and the switch to different methods:
...
Solution 2: Adapt the above to eliminate the loop with an eager extension method:
Solution 3: Adapt the above to eliminate the loop and the helper method:
您可以使用
goto
来中断跳出switch
内的循环。goto
为数不多的好用处之一...You can use
goto
in order to break out of the loop within theswitch
.One of the few good uses of
goto
...只需更改 i 的值即可:
Just change the value of i:
如果
switch
之后没有其他代码,您可以在 for 循环本身中检查是否继续循环:If there is no other code after the
switch
you can just check in the for loop itself whether to continue looping:您可以将循环放在方法内部并只使用 return 吗?
示例:
另一种选择是使用传统的
if/else
而不是switch/case
。然后你可以使用break,它会跳出你的for
循环Can you place the loop inside of a method and just use return?
Example:
Another option would be to use a traditional
if/else
instead of aswitch/case
. Then you can just use break and it will jump out of yourfor
loop您可以使用布尔值。
You could use a boolean.
我不知道循环完成后是否需要执行其他逻辑,但您可以尝试使用 return 代替。另一个选项是设置一个布尔标志,并在切换后退出。
I don't know if there's other logic that needs to be performed after the loop is finished, but you could try using return instead. The other option is to set a boolean flag, and exit after the switch.
这听起来有点糟糕的设计;有几种方法可以解决这个问题:
如果只有一个 switch 条件会破坏外部循环,那么只需在输入 switch 之前检查一下:
如果多个条件可以破坏循环,请重构为更 linq 风格的查询:
这应该调用
checkStatus
最多 50 次,直到遇到 1(或其他中断代码),然后不再继续评估元素。This smells of bad design; there are a couple of ways you could fix this:
If only one condition of your switch would break the outer loop, then simply check for that before entering the switch:
If multiple conditions can break the loop, refactor to a more linq style query:
That should call
checkStatus
up to 50 times until it encounters a 1 (or other break codes), and then doesnt continue evaluating elements.