-
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
-
It's usally advise to generate the exit condition rather than trying to force your way out of the loop, for example.
Code:
while (exit != True)
{
...
exit = True;
...
}
or
Code:
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
-
Use the break keyword;
Example:
Code:
for( ; ; ) // No termination condition.
{
if( List->AtEnd() )
break;
List->Next();
}
cout << "Control transfers to here.\n";