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.)