"break" command...where does it break to?

I have a program where I use a for loop to check an array for a succession of two numbers. The user inputs two integers, and, supposedly, the program looks through the array...if it finds the succession of numbers it alerts you, and if it doesn't find the succession, it prints the array to screen. I'll copy and paste it here:

[code=c++]
int ICs[11] = {num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11};
int x;
int y; // both x and y are defined by user input, not included here
int loop1;
int loop2;

for (loop1=0; loop1<10; loop1++) // only to loop1<10 because at ICs[10] there is no "next number" (loop1+1)
{
if (ICs[loop1] == x)
{
if (ICs[loop1+1] == y)
{
cout << "That succession of numbers is not allowed." << endl;
break;
}
}
if (loop1 == 9)
{
for (loop2=0; loop2<11; loop2++)
{
cout << ICs[loop2] << " "; // since x and y didn't occur, and loop1 is at the penultimate element of the array, this array is ok, so print it.
}
cout<< endl;
}
}
[/code]

The code itself seems to work just fine, most of the time. I've had a few glitches, so I suspect there is something wrong. As far as I can tell, the code is ok, but I'm not 100% sure of the break command at line 14.

1) Where does this "break" put the program? I assume that, if it gets to an illegal succession of numbers, it gives the cout command (line 13), and then break (line 14) takes it completely out of the for loop. Is that correct? (the for loop is, itself, contained within a big for loop...I only want it to get out of the loop shown here).

2) Can you see anything about this snippet of code that looks like it might not provide accurate results? Like I said, it seems to work about 99% of the time...I just can't figure out what went wrong the other 1% (could be another part of the program, but I don't think so...)

Thanks a lot!
1) break; exits the inner-most loop or switch statement. In this code, it jumps out of the for() loop (execution resumes on line 26)

2) I don't see anything wrong. If I had to guess it's probably a problem with getting the input from the user properly. Try printing x and y -- then try to reproduce the error. I'm wagering one of them isn't what you expect them to be.

edit: corrected "topmost"/"inner-most" so that anyone else who reads this doesn't get the wrong idea.
Last edited on
Disch -- thanks for the quick reply!

So, if this for() loop is contained within another for() loop, will it jump all the way out of that big loop?

I've really got:
[code=c++]
for (i=0; i<400; i++)
{
for (loop1=0; loop1<10; loop1++)
{
if statement #1
{
if statement #2
{
cout << "stuff";
break;
}
}
}
}
[/code]

Since you said it exits the topmost loop, I'm assuming that the break (line 10 in this pseudo-code) takes me out of BOTH loops, and resumes at line 15 (of this pseudo-code). Is that right?

If so...that's going to be a problem. :-( Is there a way to only get out of the "loop1" loop? And still loop in the "i" loop?

Thanks!
ack -- my wording choice was poor

I should have said inner-most. Sorry about that.

The break in your most recent example only exits the 'loop1' loop, not the 'i' loop. Sorry about that.
Disch --

No problem -- thanks for the clarification! Also, I think I've located the problem. You're right, it does appear to be in the user input. I'm not sure why it's a problem, but somehow, it is.

I think I've got a fix on it now. Thanks a lot!
Topic archived. No new replies allowed.