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

如何從交換機內部跳出循環?

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

問題描述

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

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 之后放置一個條件中斷來中斷循環.我只是想知道 C++ 是否已經為此提供了一些構造.

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 ) {
}

支持論據

while( true ) 循環是糟糕的形式,因為它:

Supporting Arguments

The while( true ) loop is poor form because it:

  • 打破 while 循環的隱含契約.
    • while 循環聲明應明確聲明only 退出條件.
    • Breaks the implied contract of a while loop.
      • The while loop declaration should explicitly state the only exit condition.
      • 必須閱讀循環中的代碼才能理解終止子句.
      • 永遠重復的循環會阻止用戶從程序中終止程序.
      • 有多種循環終止條件,包括檢查true".
      • 無法輕松確定將始終為每次迭代執行的代碼放在哪里.
      • 要查找錯誤、分析程序復雜性、進行安全檢查,或在不執行代碼的情況下自動導出任何其他源代碼行為,指定初始破壞條件可讓算法確定有用的不變量,從而改進自動源代碼分析指標.
      • 如果每個人都總是使用 while(true) 來表示非無限循環,那么當循環實際上沒有終止條件時,我們就失去了進行簡潔交流的能力.(可以說,這已經發生了,所以這一點沒有實際意義.)
      • 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;
      }
      

      優勢

      沒有標志.沒有goto.沒有例外.容易改變.易于閱讀.易于修復.另外代碼:

      Advantages

      No flag. No goto. No exception. Easy to change. Easy to read. Easy to fix. Additionally the code:

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

      第二點很重要.在不知道代碼如何工作的情況下,如果有人讓我讓主循環讓其他線程(或進程)有一些 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

      覆蓋執行:

      void execute() {
        super->execute();
        sleep();
      }
      

      此代碼比帶有嵌入式 switch 的循環更簡單(因此更易于閱讀).isValidState 方法應該只確定循環是否應該繼續.方法的主力應該抽象為 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 上發布的以下答案(針對 Python 問題):

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

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

      代碼

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

      對比:

      1. 初始化用戶的選擇.
      2. 循環,而用戶選擇的是重啟"這個詞.
      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.

      這篇關于如何從交換機內部跳出循環?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

      【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 亚洲成人自拍 | 久久美女视频 | 91麻豆精品国产91久久久更新资源速度超快 | 国产一区二 | 欧美精品一区在线发布 | 精品一区二区三区不卡 | 91久久精品一区二区二区 | 国产精品毛片在线 | www.日韩欧美 | 国户精品久久久久久久久久久不卡 | 精品免费国产视频 | 免费黄色片在线观看 | 色婷婷激情| 久久99精品久久久久久 | 国产精品免费一区二区 | 日本在线中文 | 亚洲精品播放 | 精品国产91久久久久久 | 9999国产精品欧美久久久久久 | 精品国产伦一区二区三区观看体验 | 久久久久久av| 一级片在线视频 | 中文字幕在线不卡播放 | 国产午夜高清 | 亚洲区一区二 | 九久久 | 国产精品久久久久久高潮 | 最新国产精品精品视频 | 久草网在线视频 | 国产精品1区2区3区 一区中文字幕 | 国产精品国产成人国产三级 | 夜夜夜夜夜夜曰天天天 | 精品日韩 | 黄色毛片在线观看 | 人人操日日干 | 美日韩一区二区 | 精品视频在线播放 | 日韩欧美在线一区二区 | 久久99精品久久久水蜜桃 | 中文字幕在线欧美 | 欧美日韩精品 |