4

How would I accomplish the following?

for (x=0;x<3;x++) {
    for (y=0;y<3;y++) {
        if (z == 1) {
            // jump out of the two for loops
        }
     }
}
// go on to do other things

If z=1, both for loops should stop and it should continue on with some other code. This obviously is an oversimplified example of what I'm trying to accomplish. (In other words, I know I need to initialize variables, etc...)

6
  • 6
    Maybe use goto or put it in a function and use return. Commented Oct 26, 2013 at 6:01
  • stackoverflow.com/questions/12612480/… Commented Oct 26, 2013 at 6:02
  • 4
    This is one situation when the use of goto might be appropriate. Commented Oct 26, 2013 at 6:05
  • 2
    I'm thinking goto at this point. The actual code is kinda messy for a break or any of the below solutiosn Commented Oct 26, 2013 at 6:08
  • Definitely a duplicate of the above question. Didn't find that one somehow. Voted to close. Commented Oct 26, 2013 at 6:09

5 Answers 5

5

Assuming you don't need the values of y and x, just assign them the values that will case both loops to exit:

for (x=0;x<3;x++) 
{
    for (y=0;y<3;y++) 
    {
        if (z == 1) 
        {
           y = 3 ;
           x = 3 ;
        }
     }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Assuming the actual code doesn't do anything interesting after the internal loop
@Leeor Yes, in that case more care should be taken.
I'm actually using the 2 for loops to go through a 2 dimensional matrix so this wouldn't really work.
@codedude how about this:stackoverflow.com/a/19603555/2327831
2

Add z to your outermost conditional expression, and break out of the innermost loop.

for(x = 0; x < 3 && z != 1; x++) {
    for(y = 0; y < 3; y++) {
        if(z == 1) {
            break;
        }
     }
 }

Of course, there's a fair bit of handwaving involved - in the code snippet you've provided, z isn't being updated. Of course, it'd have to be if this code was to work.

Comments

2
for (x=0;x<3;x++) {
    for (y=0;y<3;y++) {
        if (z == 1) {
            // jump out of the two for loops
            x=y=3; //Set the x and y to last+1 iterating value 
            break; // needed to skip over anything outside the if-condition
        }
     }
}

1 Comment

Added the needed break in the conditional block. It is needed in the case where there is something after the if-condition that does something (since he is wanting to break out of it at this point).
1

A good way to exit both loops and avoid any code that might follow the most inner loop is to put the loops in a function and return any value that you need.

for (x=0;x<3;x++) 
{
    for (y=0;y<3;y++) 
    {
        if (z == 1) 
        {
            return RETURN_VALUE
        }
        //avoids this code
    }
    //and this one too
}

2 Comments

Say I had multiple if statements like the one above inside the for loops. If the first if statement was true and it returned some value would it then skip the next if statements?
When the function returns a value the function exits.
1

Have flag, and break it

int flag=0;

for(x = 0; x < 3; x++)  
{
   for(y = 0; y < 3; y++) 
   {
       if(z == 1)
       {
          flag=1;
          break;
       }
   }
   if(flag)
     break;
 }

3 Comments

What's the point of putting z in the outermost conditional if you don't leverage it as part of the natural termination of the loop?
I didn't see that Copy paste mistake
If you flag the advantage is you break even if your logic is complex

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.