java 打破嵌套 if
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28731588/
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
Breaking out of nested if
提问by CK920
I have a bit of code that contains nested if statements:
我有一些包含嵌套 if 语句的代码:
if(numberOfNeighbors == 1){
//go through comparison again, add Pixel(i,j) to current linked list -> complist[numberOfComponents]
// break out of large check ??
if(ji.getPixelColor(i, j) == (ji.getPixelColor(i-1,j-1))){ //compare to top left
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
if(ji.getPixelColor(i, j) == (ji.getPixelColor(i,j-1))){ // compare to top
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
if(ji.getPixelColor(i, j) == (ji.getPixelColor(i+1,j-1))){ // compare to top right
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
if(ji.getPixelColor(i, j) == (ji.getPixelColor(i-1,j))){ // compare to left
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
} // end of if(numberOfNeighbors == 1)
Basically what I would like to do, however inefficient this may be, is compare a something 4 times, but if it turns out it is a match, break out of the set of the 4 nested if statements, as well as the outer if statement.
基本上我想做的,不管这可能是低效的,是比较一个东西 4 次,但如果结果它是一个匹配,打破 4 个嵌套 if 语句的集合,以及外部 if 语句.
Will this work? or will it just break out of the nested if its currently at and continue to the next until its gone through all 4?
这会奏效吗?或者如果它当前处于并继续下一个直到它完成所有 4 项,它是否会脱离嵌套?
回答by Prudhvi
IMPORTANT: break
statements are used to come out of loops but not branches
重要提示:break
语句用于退出循环但不用于分支
I understood your question but use break
statement to go out of loops like for
, while
, do while
. You can go out of if
statement when the condition is satisfied and statements inside the if
branch are executed. If you don't want to check for other conditions when your first if
is satisfied you have to use if else
branches instead of using 4 if statements.
These two links might be useful
我理解您的问题,但使用break
语句退出循环,如for
, while
, do while
。if
当条件满足并且if
分支内的语句被执行时,您可以退出语句。如果您不想在if
满足第一个条件时检查其他条件,则必须使用if else
分支而不是使用 4 个 if 语句。这两个链接可能有用
See the below example
看下面的例子
if(condition) {
if(condition) { //if this evaluates to true, logic1 is executed
logic1;
}
else if(condition) { //if the above condition fails, but this condition satisfies then logic 2 is executed
logic2;
}
else { //if the above 2 conditions fail, you can execute logic3
logic3;
}
}
回答by Samuel Edwin Ward
Are you looking for else
?
你在找else
吗?
if(numberOfNeighbors == 1){
//go through comparison again, add Pixel(i,j) to current linked list -> complist[numberOfComponents]
// break out of large check ??
if(ji.getPixelColor(i, j) == (ji.getPixelColor(i-1,j-1))){ //compare to top left
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
else if(ji.getPixelColor(i, j) == (ji.getPixelColor(i,j-1))){ // compare to top
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
else if(ji.getPixelColor(i, j) == (ji.getPixelColor(i+1,j-1))){ // compare to top right
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
else if(ji.getPixelColor(i, j) == (ji.getPixelColor(i-1,j))){ // compare to left
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
} // end of if(numberOfNeighbors == 1)
回答by Martin Clemens Bloch
break; does NOT work for if statements, only loops, switch and such.
休息; 不适用于 if 语句,仅适用于循环、开关等。
You can do this to avoid nesting:
您可以这样做以避免嵌套:
if(mainCondition)
{
if(condition1)
goto LabelContinue;
bool condition2 = logic...;
if(condition2)
goto LabelContinue;
//Code
}
LabelContinue:
//Other code.