CK920
CK920

Reputation: 21

Breaking out of nested if

I have a bit of code that contains nested if statements:

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.

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?

Upvotes: 1

Views: 25710

Answers (3)

Martin Clemens Bloch
Martin Clemens Bloch

Reputation: 1097

break; does NOT work for if statements, only loops, switch and such.

You can do this to avoid nesting:

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

  bool condition2 = logic...;

  if(condition2)
    goto LabelContinue;

  //Code
}
LabelContinue:
//Other code.

Upvotes: 0

Prudhvi
Prudhvi

Reputation: 2343

IMPORTANT: break statements are used to come out of loops but not branches

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

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

Upvotes: 9

Samuel Edwin Ward
Samuel Edwin Ward

Reputation: 6675

Are you looking for 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)

Upvotes: 3

Related Questions