【问题标题】:Technical name for switch statement without breaks无间断的 switch 语句的技术名称
【发布时间】:2010-10-12 00:06:26
【问题描述】:

有谁知道不带中断的 switch 语句的“技术名称”?

我翻了好几本教科书,在网上搜索了好久都没有结果。

【问题讨论】:

    标签: switch-statement break


    【解决方案1】:

    一个没有中断的 switch 语句(也没有循环,所以它不是 Duff 的设备),我只会调用一个 jump table

    这肯定不是结构化编程常用的工具之一。

    【讨论】:

    • 感谢您的帮助,跳转/分支表是我正在寻找的术语。
    【解决方案2】:

    当从一个 Case 子句继续执行到下一个 Case 子句时,称为“fall-through”。

    switch (i) {
        case 1:
            // do something
    
        case 2:
            // do something else
            break;
    
        case 3:
            // do another thing
    }
    

    从案例 1 到案例的执行将“失败”,但不会从案例 2 到案例 3。这是您要问的吗?

    【讨论】:

      【解决方案3】:

      失败了?

      或者您是在谈论一个没有中断的特定 switch 语句,称为Duff's Device

      send(to, from, count)
      register short *to, *from;
      register count;
      {
          register n=(count+7)/8;
          switch(count%8){
              case 0: do{ *to = *from++;
              case 7:     *to = *from++;
              case 6:     *to = *from++;
              case 5:     *to = *from++;
              case 4:     *to = *from++;
              case 3:     *to = *from++;
              case 2:     *to = *from++;
              case 1:     *to = *from++;
              }while(--n>0);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 2016-01-18
        • 1970-01-01
        • 2011-10-22
        • 1970-01-01
        • 1970-01-01
        • 2019-12-04
        相关资源
        最近更新 更多