1

我有一些包含嵌套 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 个,它是否会脱离嵌套?

4

3 回答 3

9

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

我理解你的问题,但使用break语句退出循环,如for, while, do 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;
    }
}
于 2015-02-25T23:43:50.363 回答
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)
于 2015-02-25T23:35:07.657 回答
1

休息; 不适用于 if 语句,仅适用于循环、开关等。

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

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

  bool condition2 = logic...;

  if(condition2)
    goto LabelContinue;

  //Code
}
LabelContinue:
//Other code.
于 2015-06-01T11:34:20.403 回答