如何从开关内部跳出循环? [英] How to break out of a loop from inside a switch?

查看:24
本文介绍了如何从开关内部跳出循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些看起来像这样的代码:

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**
    }
}

有什么直接的方法吗?

我知道我可以使用一个标志,并通过在切换后放置一个条件中断来中断循环.我只是想知道 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

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.
      • 必须阅读循环内的代码才能理解终止子句.
      • 永远重复的循环会阻止用户从程序中终止程序.
      • 有多个循环终止条件,包括检查真".
      • 无法轻松确定将始终在每次迭代中执行的代码放置在何处.
      • 要查找错误、程序复杂性分析、安全检查或在不执行代码的情况下自动导出任何其他源代码行为,指定初始中断条件允许算法确定有用的不变量,从而改进自动源代码分析指标.
      • 如果每个人都对非无限循环使用 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;
      }
      

      优势

      没有标志.没有转到.没有例外.容易改变.易于阅读.易于修复.另外代码:

      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.

      这篇关于如何从开关内部跳出循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆