How can I break the outer loop?

Let’s say I have nested for loops like this :

int i;
int j;

for(i = 0; i<100; i++) {
     for(j = 0; j<100; j++{
     // do something with i and j...
    if (some condition of i and j and some other variable) {
         
            //How can I break both loops right here?

}
}
}

   // So I can land right here with the values of i and j that match up with my condition...

In the code I’m working on, I’m actually four levels nested, so whatever solution has to be simple. I’m tempted to put a label at the end of the nested block and use the dreaded goto. Is there a better way?

I’m tempted to put a label at the end of the nested block and use the dreaded goto. Is there a better way?

There certainly is.

boolean keepOnGoing = true;

for(i = 0; i<100 && keepOnGoing; i++)
{
    for(j = 0; j<100 && keepOnGoing; j++)
    {
        // do something with i and j...
        if (some condition of i and j and some other variable)
        {
            // How can I break both loops right here?
            keepOnGoing = false;
        }
    }
}

Or simply set i and j to their maximum values.

Hardware engineers learning to think outside the loop box 8)

AWOL:
Or simply set i and j to their maximum values.

But doesn't that mean that

// So I can land right here with the values of i and j that match up with my condition...

The values of i and j wouldn't be preserved?

Regards,

Brad
KF7FER

I thought that it was Chicago real-estate agents who thought outside The Loop?

They'd be preserved, if you preserved them.
Think about it, you can do (potentially) 10000 superfluous comparisons, or you can do two quick saves.
Which would you do?

Once you've left the loop, why would you care what the i and j values are ? Are you planning to jump back into the middle of the loop after leaving for lunch ?

why would you care what the i and j values are

So that you know which members of your matrix to address?
You know, the ones that gave you the answer you wanted.

Ok, now you lost me . What matrix. I don't see where the OP stated what he doing. All the comments are vague. Can you tell me what these nested FOR loops are for ? What do you mean by matrix ? (I know what a matrix is I just don't know why it is important here because I don't know what it represents )

AWOL:
They’d be preserved, if you preserved them.

True. Technically speaking you’re talking about saving the values within i and j, not the variables i and j which is what the code asked for, right?

Just trying to split a very small hair… :wink:

Think about it, you can do (potentially) 10000 superfluous comparisons, or you can do two quick saves.
Which would you do?

When you put it that way, I agree.

Though my brain doesn’t like it for some reason; it seems dirty to manually mess with the index values like that. Plus if I use a boolean in the conditional, I can tell with a quick glance that the loop will exit early. Must be my desire for order despite the cost (and my non-embedded background).

Regards,

Brad
KF7FER

raschemmel:
Ok, now you lost me . What matrix. I don't see where the OP stated what he doing. All the comments are vague. Can you tell me what these nested FOR loops are for ? What do you mean by matrix ? (I know what a matrix is I just don't know why it is important here because I don't know what it represents )

It really doesn't matter, does it?
S/he wants to break out of a loop or loops.
Does it matter if the reason for the breakout is a pattern match of an area correlator, which causes the cruise missile to enter another phase of target acquisition, knowing where the target is in the source image, or simply that you've got a database hit, or that you've simply found the first occurrence of the thing you're looking for?

It's simply an abstract problem, with lots of solutions.

it seems dirty to manually mess with...

keepOnGoing = false;
Or
j=100;
Not much difference.

Must be my desire for order...

I have to straighten every picture when I walk by one, and a whole bunch of other things too, oh we'll...

raschemmel:
Once you've left the loop, why would you care what the i and j values are ? Are you planning to jump back into the middle of the loop after leaving for lunch ?

They will be preserved if they are declared outside the for loop, for as long as they are still in scope. You did notice that the loop counters are declared before the loop is entered, right? There are gazillions of reasons you might want to know the values of loop counters after the loop exits.

@raschemmel: You're either over- or under-thinking things, but hopefully when you've got some more programming experience, you'll see the point of the original question.
Or maybe you're just bumping your post count, again.

Think about it, you can do (potentially) 10000 superfluous comparisons, or you can do two quick saves.
Which would you do?

Where do these (potentially) superfluous comparisons happen? When the continue condition is no longer true, because i or j exceeded the maximum values, or because keepOnGoing became false, the inner loop terminates, followed, on the next iteration, by the outer loop.

I didn't notice the red warning mesage at the top of the screen saying :

Well, now that you have...

Thanks all. I didn't mean to stir up a big conversation.

The code is looking through a big matrix. In the real code it is 4 dimensional so there are 4 levels of nested loop in the search structure. When I find the first match I leave and I want to know the "coordinates" of the match I found (so the missile lands on target :slight_smile: ) or that all of the iterators are at their maximum value so no match was found. I had thought of the solution in Pauls first response and it seemed logical but bulky. There is more code after the conditional so I'd have to set the boolean and then call break to break the innermost loop. That sounds easy enough.

I was just wondering if I had found the elusive "one use" for the goto.

Delta_G:
The code is looking through a big matrix. In the real code it is 4 dimensional so there are 4 levels of nested loop in the search structure. When I find the first match I leave and I want to know the “coordinates” of the match I found (so the missile lands on target :slight_smile: ) or that all of the iterators are at their maximum value so no match was found. I had thought of the solution in Pauls first response and it seemed logical but bulky. There is more code after the conditional so I’d have to set the boolean and then call break to break the innermost loop. That sounds easy enough.

Assume you have 4 nested loops. External to any of the loops, declare a bool ((let’s call it getOuttaDodge), and set it to false.

If you find something in any of the loops, set that bool to true, and follow it with a break statement.

Right after the closing curly brace on all of the inner loops, check getOuttaDodge.

If true, issue another break.

When you drop out of the outermost loop, check the bool.

If it’s true, you found something interesting, and your loop variables point to it. Otherwise, you didn’t find anything intereting

  int i, j, k, m
    bool getOuttaDodge;
  for ( i=0; i< 10; i++) {
    for ( j=0; i< 10; i++) {
      for (ki=0; i< 10; i++) {
        for (mi=0; i< 10; i++) {
          if (foundsomething) {
            getOuttaDodge = true;
            break;
          }
        }
        if (getOuttaDodge) break;
      }
      if (getOuttaDodge) break;
    }
    if (getOuttaDodge) break;
  }
  if (getOuttaDodge) {
    got_one()
    }

I was just wondering if I had found the elusive “one use” for the goto.

Not even close! :roll_eyes:

I think you’re after something like this, using 10x10x10x10 as an example.

for (w=0; w<10; w=w+1){
  for (x=0; x<10; x=x+1){
     for (y=0; y<10; y=y+1){
       for (z=0; z<10; z=z+1){
            if (array[w,x,y,z] == searchValue){
               locationw = w;
               locationx = x;
               locationy = y;
               locationz = z;
              w = 10;
              x = 10;
              y = 10;
              z = 10;
              } // end search compare
            } //end z
           } // end y
         } // end x
      } // end x