java 可以对外循环使用 break 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16769838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Possible to use break for outer loop?
提问by 3D-kreativ
If I use break
like the code below, then the loop within row
won't iterate the rest if there is a match in the beginning, but what about the col
loop?
如果我break
像下面的代码一样使用,那么row
如果开头有匹配项,则其中的循环将不会迭代其余部分,但是col
循环呢?
Would it still iterate between 0 and 7? Is there a way to use break
there as well?
它还会在 0 和 7 之间迭代吗?有没有办法在break
那里使用?
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something)
{
//Then do this;
break;
}
回答by Jon Skeet
One option is to use a condition flag. You could then eitherbreak in the outer loop as well, or just use it as an extra condition within the for
loops:
一种选择是使用条件标志。然后,您也可以中断外部循环,或者仅将其用作for
循环中的额外条件:
bool keepGoing = true;
for (int col = 0; col < 8 && keepGoing; col++)
{
for (int row = 0; row < 8 && keepGoing; row++)
{
if (something)
{
// Do whatever
keepGoing = false;
}
}
}
In Java you can specify a label to break to though. (I didn't see that this question was tagged Java as well as C#.)
在 Java 中,您可以指定要中断的标签。(我没有看到这个问题被标记为 Java 和 C#。)
outerLoop:
for (...)
{
for (...)
{
if (...)
{
break outerLoop;
}
}
}
EDIT: As noted in comments, in C# you coulduse a label and goto
:
编辑:如评论中所述,在 C# 中,您可以使用标签和goto
:
for (...)
{
for (...)
{
if (...)
{
goto endOfLoop;
}
}
}
endOfLoop:
// Other code
I'd reallyrecommend that you don't take either of these approaches though.
不过,我真的建议您不要采用这两种方法中的任何一种。
In both languages it would usually be best to simply turn both loops into a single method - then you can just return from the method:
在这两种语言中,通常最好将两个循环简单地转换为一个方法 - 然后您可以从该方法返回:
public void doSomethingToFirstOccurrence()
{
for (...)
{
for (...)
{
if (...)
{
return;
}
}
}
}
回答by anshulkatta
Yes, it is possible by using a break
label:
是的,可以使用break
标签:
package others;
public class A {
public static void main(String[] args) {
outer: for(int col = 0; col < 8; col ++)
{
for (int row = 0; row < 8; row ++)
{
if (col == 4)
{
System.out.println("hi");
break outer;
}
}
}
}
}
回答by Subhrajyoti Majumder
You can put logic like this:
你可以像这样放置逻辑:
boolean condition = false;
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something) {
// Then do this:
condition = true; // Break condition for outer loop
break;
}
}
if (condition)
break;
}
回答by Fabio Marcolini
It doesn't exit the col loop.
它不会退出 col 循环。
Instead, you can wrap all in a function and use return;
to exit immediately from the loop
相反,您可以将所有内容包装在一个函数中并用于return;
立即退出循环
private Xy Loop( /* Parameters */)
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something) {
// Then do this;
return something; //Or just return;
}
}
}
}
回答by Marco Forberg
break
only breaks the loop that is directly around it. You could use a flag to control the outer loop:
break
只会打破直接围绕它的循环。您可以使用标志来控制外循环:
boolean continueOuterLoop = true;
for(int col = 0; continueOuterLoop && col < 8; col ++) {
for(int row = 0; row < 8; row ++) {
if(check something) {
//Then do this;
continueOuterLoop = false;
break;
}
}
}
回答by Michael 'Maik' Ardan
nameHere:
for (yourForLoop) {
for (anotherLoop) {
if(condition) {
break nameHere;
}
}
}
回答by anddevmanu
I think you should use a tag or a label, like "outerLoop". This works in Java:
我认为您应该使用标签或标签,例如“outerLoop”。这适用于 Java:
outerLoop:
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something)
{
//Then do this;
break outerLoop;
}
回答by Aristos
One more alternative to the other answers is to set your counters to the maximum, to stop the loops.
其他答案的另一种替代方法是将计数器设置为最大值,以停止循环。
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something)
{
// Use the col and row here.
// Now we go for a totally break of all loops.
// To stop the loops you can set that to the maximum
// of your loop test.
row = 8;
col = 8;
}
The advantage to that trick is that you do not add any additional checking code to the full loop and that makes it a lot faster.
该技巧的优点是您无需向完整循环添加任何额外的检查代码,从而使其速度更快。
回答by Peter Olson
In Java you can use a break label
在 Java 中,您可以使用 break label
outer:
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something)
{
break outer;
}
}
}
And, since nobody else has mentioned it yet, in C# you can use goto label
而且,由于还没有其他人提到它,在 C# 中你可以使用 goto label
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something)
{
goto outside;
}
}
}
outside:
回答by Ramesh
Loop1:
for (int col = 0; col < 8; col ++)
{
for (int row = 0; row < 8; row ++)
{
if (condition)
{
break Loop1;
}
}
}
This could do what you need...
这可以做你需要的......