Immediate exit of 'while' loop in C++


Question

How do I exit a while loop immediately without going to the end of the block?

For example,

while (choice != 99)
{
    cin >> choice;
    if (choice == 99)
        //Exit here and don't get additional input
    cin>>gNum;
}

Any ideas?

1
13
7/19/2015 5:32:32 PM

Accepted Answer

Use break?

while(choice!=99)
{
  cin>>choice;
  if (choice==99)
    break;
  cin>>gNum;
}
51
5/16/2009 6:32:09 PM

cin >> choice;
while(choice!=99) {
    cin>>gNum;
    cin >> choice
}

You don't need a break, in that case.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon