如何从交换机内部断开环路? [英] How to break out of a loop from inside a switch?

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

问题描述

我写的代码看起来像这样:

  while(true){
switch - > state){
case MSGTYPE:// ...
break;
// ...更多的东西...
case DONE:
break; // ** HERE,我想打破循环本身**
}
}


b $ b

有没有什么直接的方法可以做到这一点?



我知道我可以使用一个标志,开关。我只想知道C ++是否已经有了这个结构。

解决方案

前提



以下代码应被视为不良形式,无论语言或所需的功能如何:

  while 
}



辅助参数



while(true)循环是不好的形式,因为它:




  • >打破while循环的隐含合约。

    • while循环声明应明确说明退出条件。



必须读取循环才能理解终止子句。 / li>
  • 效率不高。

    • 有多个循环终止条件,包括检查true li>

  • 容易出现错误。

    • 导致不必要的复杂代码。


    替代转到



    以下代码更好: / p>

      while(isValidState()){
    execute();
    }

    bool isValidState(){
    return msg-> state!= DONE;
    }



    优点



    没有标志。否 goto 。没有例外。容易改变。易于阅读。易于修复。另外,代码如下:


    1. 从循环本身中分离循环工作负载的知识。


    2. 允许在一个地方分配多个终止条件。

    3. 将终止子句与代码分开

    4. 对核电厂更安全。 ; - )

    第二点很重要。不知道代码的工作原理,如果有人要求我让主循环让其他线程(或进程)有一些CPU时间,两个解决方案:



    选项#1



    轻松插入暂停:

      while(isValidState )){
    execute();
    sleep();
    }



    选项2



    覆盖执行:

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

    此代码比嵌入式循环更简单 switch isValidState 方法应该只确定循环是否应该继续。该方法的主力应该被抽象到 execute 方法中,这允许子类重写默认行为(使用嵌入的 switch goto )。



    Python示例


    $ b b

    对比在StackOverflow上发布的以下答案(对Python问题):


    1. 永远循环。
    2. $
    3. 如果用户的输入是重新启动,则继续循环。

    4. 否则, ,停止循环。

    5. 结束。



    代码

      while True:
    choice = raw_input('你想要什么?')

    如果选择=='restart':
    continue
    else:
    break

    print'Break!'

    Versus:


    1. 初始化用户的选择。

    2. 当用户选择


    3. 要求使用者输入他们的选择。


    4. $ b代码

        choice ='restart'; 

      while choice =='restart':
      choice = raw_input('你想要什么?')

      print'Break!'

      这里, while True 会导致误导和过于复杂的代码。 / p>

      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?

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

      Supporting Arguments

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

      • Breaks the implied contract of a while loop.
        • The while loop declaration should explicitly state the only exit condition.
      • Implies that it loops forever.
        • Code within the loop must be read to understand the terminating clause.
        • Loops that repeat forever prevent the user from terminating the program from within the program.
      • Is inefficient.
        • There are multiple loop termination conditions, including checking for "true".
      • Is prone to bugs.
        • Cannot easily determine where to put code that will always execute for each iteration.
      • Leads to unnecessarily complex code.

      Alternative to "Go To"

      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. Isolates the knowledge of the loop's workload from the loop itself.
      2. Allows someone maintaining the code to easily extend the functionality.
      3. Allows multiple terminating conditions to be assigned in one place.
      4. Separates the terminating clause from the code to execute.
      5. Is safer for Nuclear Power plants. ;-)

      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:

      Option #1

      Readily insert the pause:

      while( isValidState() ) {
        execute();
        sleep();
      }
      

      Option #2

      Override execute:

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

      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).

      Python Example

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

      1. Loop forever.
      2. Ask the user to input their choice.
      3. If the user's input is 'restart', continue looping forever.
      4. Otherwise, stop looping forever.
      5. End.

      Code

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

      Versus:

      1. Initialize the user's choice.
      2. Loop while the user's choice is the word 'restart'.
      3. Ask the user to input their choice.
      4. End.

      Code

      choice = 'restart';
      
      while choice == 'restart': 
          choice = raw_input('What do you want? ')
      
      print 'Break!'
      

      Here, while True results in misleading and overly complex code.

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

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