Question about how to rewrite this loop

I think I might be having a brain fart, but I can't figure this out. I have a loop that has the following form:

1
2
3
4
5
6
7
8
9
10
11
12
while (num1 != num2){
	if (num1 == num3){
	// do some stuff
	break; // now break since we don't care about looping till num1 == num2
	}
	++num1;
}

if (num1 == num2){ // If we got to the end of last loop, we didn't do anything in the above if statement
	// so then do these statements...
}


I was wondering how I could skip the if statement after the while loop if we did happen to break out of it early since we know it will be false anyway if we did break early. I know I could set a variable before the break and check for this before the next if statement to skip it, but I'd rather land past the following if statement if we were to break. I know I could do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
while (num1 != num2){
	if (num1 == num3){
	// do some stuff
	goto label; // now break since we don't care about looping till num1 == num2
	}
	++num1;
}

if (num1 == num2){ // If we got to the end of last loop, we didn't do anything in the above if statement
	// so then do these statements...
}
label:


But I've heard using goto's can be bad. So is there a way I can rewrite this without the goto?
Do you really need the side effect of incrementing num1?
1
2
3
4
5
6
7
8
    if (num1 <= num3 && num3 < num2)
    {
        //do some stuff that you originally had in the while loop
    }
    else
    {
        //do the stuff you were originally going to do in the if block
    }
Last edited on
I guess the form I showed doesn't exactly fit what I'm doing. Here is the actual piece of code where begIt and endIt are vector iterators.

1
2
3
4
5
6
7
8
9
10
while (begIt != endIt) {
          if ((*begIt).member1 == someObject.member1){
          // Do some statements
          break; 
          }
          ++begIt; 
}
if (begIt == endIt){ // no equal member was found so do these statements instead
         // Do some statements.
}


Basically I'm iterating through a vector checking the objects members of it to see if a certain one has an equal member of another object's member. If so, then break. If I break, I know that the next if statement will fail, so why check it? Thats why I'm just trying to see if there is a way to go passed it without using a goto.

Hope that clarifies it a bit more.
Last edited on
Ok I have come up with one solution and that is to make this section of the code a function and replace the break; with a return; and then just remove the if conditional after the loop and just have the statements that would normally come after that condition there.

However this means I now have to have an ugly function argument list with a bunch of references being passed to it from main, or else make all those variables/objects in main global.

Still looking for a different solution. Could this be one case where the goto statement is acceptable though?
Topic archived. No new replies allowed.