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

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

问题描述

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

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.

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

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