突破嵌套if

时间:2015-02-25 23:17:16

标签: java if-statement break nested-if

我有一些包含嵌套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)

基本上我想做的事情,无论多么低效,都是比较一下4次,但如果事实证明它是匹配的,那么就要突破4个嵌套if语句的集合,以及外部if语句。

这会有用吗?或者它是否会突破嵌套,如果它当前处于并继续到下一个直到它通过所有4?

3 个答案:

答案 0 :(得分:8)

重要提示:break语句用于循环而不是分支

我理解了您的问题,但使用break语句来消除forwhiledo while等循环。当满足条件并执行if分支内的语句时,您可以退出if语句。如果您不想在满足第一个if时检查其他条件,则必须使用if else个分支,而不是使用4个if语句。 这两个链接可能很有用

见下面的例子

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;
    }
}

答案 1 :(得分:3)

您在寻找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)

答案 2 :(得分:2)

中断;不适用于if语句,只适用于循环,切换等。

您可以这样做以避免嵌套:

if(mainCondition)
{
  if(condition1)
    goto LabelContinue;

  bool condition2 = logic...;

  if(condition2)
    goto LabelContinue;

  //Code
}
LabelContinue:
//Other code.