PDA

Click to See Complete Forum and Search --> : while loops . . .


TheLeeMan
Oct 5th, 2000, 02:25 PM
Hey -

I had a simple question concerning while loops. I need to be able to have code inside a loop exit the loop when a certain condition is met. An example would be the VB code, "exit while". Is there a C++ equivilent? Thanks for your help . . .

TheLeeMan

kb244
Oct 5th, 2000, 02:50 PM
It's usally advise to generate the exit condition rather than trying to force your way out of the loop, for example.


while (exit != True)
{
...
exit = True;
...
}


or


do
{
...
exit = true;
...
} while(exit != True)


Depending on how soon you want to exit, or test for a condition, you can use either a Pre-Test Loop, or a Post-Test loop.

but I think you can use

break;

Vlatko
Oct 5th, 2000, 03:44 PM
Use the break keyword;
Example:

for( ; ; ) // No termination condition.
{
if( List->AtEnd() )
break;

List->Next();
}

cout << "Control transfers to here.\n";