久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何從交換機內(nèi)部跳出循環(huán)?

How to break out of a loop from inside a switch?(如何從交換機內(nèi)部跳出循環(huán)?)
本文介紹了如何從交換機內(nèi)部跳出循環(huán)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在編寫一些看起來像這樣的代碼:

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:

      1. 將循環(huán)工作負載的知識與循環(huán)本身隔離開來.
      2. 允許維護代碼的人員輕松擴展功能.
      3. 允許在一處分配多個終止條件.
      4. 將終止子句與要執(zhí)行的代碼分開.
      5. 對核電站來說更安全.;-)

      第二點很重要.在不知道代碼如何工作的情況下,如果有人讓我讓主循環(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 方法,它允許子類覆蓋默認行為(使用嵌入式 switchgoto).

      This code is simpler (thus easier to read) than a loop with an embedded switch. The isValidState method should only determine if the loop should continue. The workhorse of the method should be abstracted into the execute method, which allows subclasses to override the default behaviour (a difficult task using an embedded switch and goto).

      對比 StackOverflow 上發(fā)布的以下答案(針對 Python 問題):

      Contrast the following answer (to a Python question) that was posted on StackOverflow:

      1. 永遠循環(huán).
      2. 請用戶輸入他們的選擇.
      3. 如果用戶的輸入是重啟",則永遠繼續(xù)循環(huán).
      4. 否則,永遠停止循環(huán).
      5. 結束.

      代碼

      while True: 
          choice = raw_input('What do you want? ')
      
          if choice == 'restart':
              continue
          else:
              break
      
      print 'Break!' 
      

      對比:

      1. 初始化用戶的選擇.
      2. 循環(huán),而用戶選擇的是重啟"這個詞.
      3. 請用戶輸入他們的選擇.
      4. 結束.

      代碼

      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)系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 美女久久久 | 欧美日韩亚洲一区 | 伊人精品视频 | 奇米影视77| 中文在线一区二区 | 久久不卡 | 亚洲午夜精品视频 | 中文字幕国 | 韩国av网站在线观看 | 精品久久中文字幕 | 色婷婷久久久久swag精品 | 成人三级视频 | 一区二区三区高清不卡 | 成人免费视频网站在线观看 | 久久99国产精品久久99果冻传媒 | 日韩高清一区 | 国产精品综合网 | 午夜精品一区二区三区在线视频 | 精品亚洲一区二区 | 欧美日韩一区二区三区四区 | 日本三级网站在线 | 精品一区二区电影 | 国产精品视频导航 | 欧美午夜激情在线 | 欧美一二三四成人免费视频 | 手机在线一区二区三区 | 福利二区| 国产精品福利一区二区三区 | 国产福利在线播放麻豆 | 欧美精品乱码久久久久久按摩 | 精品一区二区电影 | 亚洲精品黄 | 狠狠综合网 | 欧美精品一区二区在线观看 | 亚洲视频中文字幕 | 污免费网站 | 亚洲综合色丁香婷婷六月图片 | 亚洲色图在线观看 | a久久 | 色吧色综合 | 成人在线精品视频 |