这是我的代码架构:
while (..)
{
for (...; ...;...)
for(...;...;...)
if ( )
{
...
continue;
}
}
继续做什么?他只会让第二个循环迭代一次,不是吗?我希望它达到一段时间,这可能吗?
谢谢!
这里continue
影响最近的循环 - 你的第二个for
。直接跳转有两种方式while
:
goto
,虽然有时“被认为是有害的”,但这可以说是它仍然存在的主要原因return
为了说明后者:
while (..)
{
DoSomething(..);
}
void DoSomething(..) {
for (...; ...;...)
for(...;...;...)
if ( )
{
...
return;
}
}
和前者:
while (..)
{
for (...; ...;...)
for(...;...;...)
if ( )
{
...
goto continueWhile;
}
continueWhile:
{ } // needs to be something after a label
}
while (..)
{
for (...; ...;...)
for(...;...;...)
if ( )
{
...
goto superpoint;
}
superpoint:
//dosomething
}
您应该设置一个变量来确定何时需要离开循环。
while (..)
{
bool goToWhile = false;
for (...; ... && !goToWhile; ...)
for (...; ... && !goToWhile; ...)
if ( )
{
...
goToWhile = true;
}
}
不过想出更好的名字;)
不可能直接因为continue;
只继续执行当前循环,要转到外循环,你唯一能做的就是设置一些标志并在外循环中检查它
continue
orbreak
总是用于接受 a continue
or的最内层循环break
。在这种情况下,它是代码中最低的for
循环。
仅使用 continue 语句是不可能的。Continue 和 break 语句只影响它们嵌套的最内层循环。
您可以设置一个变量并在外循环中检查它。或者重新组织for语句中的IF语句和break条件。
while (..)
{
for (...; ...;...)
{
for(...;...;...)
if ( )
{
...
skipLoop = true
}
if (skipLoop)
continue;
}
}
希望这可以帮助!