問題描述
我正在編寫一些看起來像這樣的代碼:
I'm writing some code that looks like this:
while(true) {
switch(msg->state) {
case MSGTYPE: // ...
break;
// ... more stuff ...
case DONE:
break; // **HERE, I want to break out of the loop itself**
}
}
有沒有直接的方法可以做到這一點?
Is there any direct way to do that?
我知道我可以使用一個標志,并通過在 switch 之后放置一個條件中斷來中斷循環(huán).我只是想知道 C++ 是否已經(jīng)為此提供了一些構造.
I know I can use a flag, and break from the loop by putting a conditional break just after the switch. I just want to know if C++ has some construct for this already.
推薦答案
Premise
無論語言或所需功能如何,以下代碼都應被視為不良形式:
Premise
The following code should be considered bad form, regardless of language or desired functionality:
while( true ) {
}
支持論據(jù)
while( true )
循環(huán)是糟糕的形式,因為它:
Supporting Arguments
The while( true )
loop is poor form because it:
- 打破 while 循環(huán)的隱含契約.
- while 循環(huán)聲明應明確聲明only 退出條件.
- Breaks the implied contract of a while loop.
- The while loop declaration should explicitly state the only exit condition.
- 必須閱讀循環(huán)中的代碼才能理解終止子句.
- 永遠重復的循環(huán)會阻止用戶從程序中終止程序.
- 有多種循環(huán)終止條件,包括檢查true".
- 無法輕松確定將始終為每次迭代執(zhí)行的代碼放在哪里.
- 要查找錯誤、分析程序復雜性、進行安全檢查,或在不執(zhí)行代碼的情況下自動導出任何其他源代碼行為,指定初始破壞條件可讓算法確定有用的不變量,從而改進自動源代碼分析指標.
- 如果每個人都總是使用
while(true)
來表示非無限循環(huán),那么當循環(huán)實際上沒有終止條件時,我們就失去了進行簡潔交流的能力.(可以說,這已經(jīng)發(fā)生了,所以這一點沒有實際意義.)
- If everyone always uses
while(true)
for loops that are not infinite, we lose the ability to concisely communicate when loops actually have no terminating condition. (Arguably, this has already happened, so the point is moot.)
以下代碼是更好的形式:
The following code is better form:
while( isValidState() ) { execute(); } bool isValidState() { return msg->state != DONE; }
優(yōu)勢
沒有標志.沒有
goto
.沒有例外.容易改變.易于閱讀.易于修復.另外代碼:Advantages
No flag. No
goto
. No exception. Easy to change. Easy to read. Easy to fix. Additionally the code:- 將循環(huán)工作負載的知識與循環(huán)本身隔離開來.
- 允許維護代碼的人員輕松擴展功能.
- 允許在一處分配多個終止條件.
- 將終止子句與要執(zhí)行的代碼分開.
- 對核電站來說更安全.;-)
第二點很重要.在不知道代碼如何工作的情況下,如果有人讓我讓主循環(huán)讓其他線程(或進程)有一些 CPU 時間,我會想到兩種解決方案:
The second point is important. Without knowing how the code works, if someone asked me to make the main loop let other threads (or processes) have some CPU time, two solutions come to mind:
隨時插入停頓:
while( isValidState() ) { execute(); sleep(); }
選項#2
覆蓋執(zhí)行:
void execute() { super->execute(); sleep(); }
此代碼比帶有嵌入式
switch
的循環(huán)更簡單(因此更易于閱讀).isValidState
方法應該只確定循環(huán)是否應該繼續(xù).方法的主力應該抽象為execute
方法,它允許子類覆蓋默認行為(使用嵌入式switch
和goto代碼>).
This code is simpler (thus easier to read) than a loop with an embedded
switch
. TheisValidState
method should only determine if the loop should continue. The workhorse of the method should be abstracted into theexecute
method, which allows subclasses to override the default behaviour (a difficult task using an embeddedswitch
andgoto
).對比 StackOverflow 上發(fā)布的以下答案(針對 Python 問題):
Contrast the following answer (to a Python question) that was posted on StackOverflow:
- 永遠循環(huán).
- 請用戶輸入他們的選擇.
- 如果用戶的輸入是重啟",則永遠繼續(xù)循環(huán).
- 否則,永遠停止循環(huán).
- 結束.
代碼
while True: choice = raw_input('What do you want? ') if choice == 'restart': continue else: break print 'Break!'
對比:
- 初始化用戶的選擇.
- 循環(huán),而用戶選擇的是重啟"這個詞.
- 請用戶輸入他們的選擇.
- 結束.
代碼
choice = 'restart'; while choice == 'restart': choice = raw_input('What do you want? ') print 'Break!'
在這里,
while True
會導致誤導和過于復雜的代碼.Here,
while True
results in misleading and overly complex code.這篇關于如何從交換機內(nèi)部跳出循環(huán)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!