Mei*_*eir 13 c++ break while-loop
如何在while
不进入块结束的情况下立即退出循环?
例如,
while (choice != 99)
{
cin >> choice;
if (choice == 99)
//Exit here and don't get additional input
cin>>gNum;
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
And*_*mar 52
用休息?
while(choice!=99)
{
cin>>choice;
if (choice==99)
break;
cin>>gNum;
}
Run Code Online (Sandbox Code Playgroud)
cin >> choice;
while(choice!=99) {
cin>>gNum;
cin >> choice
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,你不需要休息.
使用break,因此:
while(choice!=99)
{
cin>>choice;
if (choice==99)
break; //exit here and don't get additional input
cin>>gNum;
}
Run Code Online (Sandbox Code Playgroud)
这也适用于for循环,并且是结束switch子句的关键字.更多信息在这里.