get out of the loops?

Pages: 12
hi, is there a way to get out of a loop from a loop or a function inside that loop?


1
2
3
4
5
6
7
8
9
10
while(.....)
{
for(.....)
  {
    ...is there a way to get out of the while from here?
  }
}




Yea by using very poor and evil techniques called goto, break and continue. I strongly suggest that you write a better loop condition by using switches and if/else rather than goto/break/continue.

In a switch/case break is a must and not as evil.
kenshee wrote:
Yea by using very poor and evil techniques called goto, break and continue. I strongly suggest that you write a better loop condition by using switches and if/else rather than goto/break/continue.

In a switch/case break is a must and not as evil.


I don't see why break / continue are evil techniques, care to explain?
Here one example of "evil techniques" ^^
1
2
3
4
5
6
7
8
9
10
11
12
13
bool leave = false;
while(.....)
{
  for(.....)
  {
    if(whatyouwonnareach)
    {
      leave = true;
      break; // leave for-loop
    }
  }
  if(leave)break; // leave while-loop
}
A break is the only way to exit from the middle of the loop block, there's nothing evil on it.

In a switch/case break is a must and not as evil.
If you hate jumps this much you should hate switches too
Guys, you're missing something:

1
2
3
4
5
6
7
while(.....)
{
  for(.....)
  {
    ...is there a way to get out of the while from here?
  }
}

This is one of those very few legitimate uses or goto:

1
2
3
4
5
6
7
8
9
10
while(.....)
{
  for(.....)
  {
    if(.....)
      goto skip_loop;
  }
}

skip_loop:

Or you can just place the loops in a separate function, and return.
Last edited on
I didn't miss it. If this line if(leave)break; // leave while-loop is directly behind the for-loop it does the same like your goto
Oh yes, now I see it.

In fact, my mistake is exactly the reason why you shouldn't over-compilcate the code only to avoid goto:)
i heard goto and break are evil ways to exit a loop because they are poor design. if we had to exit in the middle of a for(), then we were trying to use a for() when a while() fit the situation. that sort of thing.

the code also gets harder to follow when the exits are not in the usual places. completely removing the usual exit is clearer. we know to look elsewhere and we know there could be more than one:
1
2
3
4
5
while(1) { // loop doesn't end here. end is inside the loop
	// blah blah blah
	if(something) break; // here is one of them. are there more? keep looking.
	// blah blah blah
} // end of the loop. guess that was the only break. 

Ideally there is one way into the loop (reaching it in code) and one way out (violating the conditions set forth in the loop's code). Any other exits could be considered bad programming (I would personally say it is.). (Obviously switch is an exception to forbidding break because break is nearly necessary for most switch situations.)
If you want to exit from the 'middle' of the loop, there is a technique I learned in an old programming class called priming. Basically, you perform some task out of the loop and again at the end of the loop, just before the condition is checked.
1
2
3
4
5
6
7
char input;
(while input != 'y')
{
cout << "Do you want to exit?";
cin >> input;
cout << "So you don't want to exit.";
}


Overlooking the obvious uselessness of this code, the idea is that you only print out the second statement if the user does not input a y. But the condition is checked after this printout so inevitably it will be printed before the loop ends.
You could slap the statement into an if so that it only executes if the char is not y. Alternatively, you could prime the loop:
1
2
3
4
5
6
7
8
9
char input;
cout << "Do you want to exit?";
cin >> input;
(while input != 'y')
{
cout << "So you don't want to exit.";
cout << "Do you want to exit?";
cin >> input;
}

In other words, you modify the flow of your loop so the condition is checked right after the input. Whether or not this is more efficient than an if depends on the situation but I would say it's better style than using breaks.

EDIT:
By the way, you can't exit the while directly from a for nested within it, or any other pair of loops in that setup; you'd need a break in one loop and another break in the outer one. (Besides the fact that that's really bad programming, I can't conceive a situation in which you'd need to do that.)
Last edited on
I would say that typing twice the code only to avoid the use of break is bad programming
Agreed.
Bazzy wrote:
I would say that typing twice the code only to avoid the use of break is bad programming


Agreed.
agreed x 3
agreed x4 :)

I really don't get those "xxx is evil, never use it" thing. In my opinion, when you decide which technique to use, you should just choose the technique that will make the code easiest to understand (I assume that other factors, like efficiency, are not important here). Sometimes the use of break can make the code more readable, sometimes the opposite. So stop making those "never/always" rules please.


agreed x5

To refuse break/continue is a prejudice and witch-hunt. I'd even use goto to go out of 2- and more-deep cycles if i'm sure that the code structure is clear enough to not admit any evil with it. A (approximate) cite from one book: "Goto is the only effective way to go out of deeply nested loops".

Good compilers make warning if a goto may introduce any mess in your code (jumps from one code block to another weak-related block or smth.). Though deep cycles is the only point i give goto chance to live.
agreed x6

To be honest I always find a goto complicates things...I can't read it as easily, but then again I don't usually nest loops too deep, and they have a reason for being nested.
tummychow, yep, very old school. (by the way, your example fits a do-while())

new school:
1
2
3
4
for(;1;) {
	if(whatever) goto stop;
}
stop:

mmmm. good programming.
There you should use a break
goto? In this day and age people still even think of using goto, never mind recomending it?
Pages: 12