【问题标题】:Difference between "no statement" and continue"no statement" 和 continue 之间的区别
【发布时间】:2014-12-31 22:39:18
【问题描述】:

在下面的代码中,请查看带有“continue”语句的部分。如果我删除 'continue' 语句并且我什么都没有代替它会有什么不同。

int prime_or_not(int x){
    int i;
    if (x == 1 || x == 2){
        return 1;
    }else if (x % 2 == 0){
        return 0;
    }else if (x > 2){
        for (i = 3; i < x; i += 2){
            if (x % i == 0){
                return 0;
                break;
            }else {
                continue;
            }
        }
        return 1;
    }else {
        return 0;
    }
}

【问题讨论】:

  • 在调试器中单步执行你发现了什么?
  • breakcontinue 都不是必需的。
  • 顺便说一句,1 不是素数。
  • 有些人以“最佳实践”的名义总是为每个if 拥有一个else。在您显示的代码中,给定的else { continue; }、一个空的else {} 和根本没有else 都会产生相同的效果。此外,在return 之后放置一个中断是无法访问的代码。
  • 关于这一行:'for (i = 3; i >1); i += 2){'

标签: c continue


【解决方案1】:

这根本没有区别。 continue 语句是跳转语句。基本上,它会跳回你的循环而不执行它之后的代码。 由于continue 语句是在您的循环中最后执行的,因此它没有任何效果。

【讨论】:

    【解决方案2】:

    这对您的代码没有任何影响,但请考虑这样的事情:

    (伪代码):

    for(int i = 10; i > -5; i--) {
        if(i == 0)
            continue;
    
        printf("%d", 10/i);
    }
    

    【讨论】:

      【解决方案3】:

      'continue' 直接跳转到'for' 或'while' 括号“}”的末尾。在您的情况下,由于 continue 关键字之后没有任何内容,因此没有区别。

      为了清楚起见,通常情况下,“继续”和不声明之间存在很大差异。例如,

      for (code-condition){
       code-1
       code-2
       continue;
       code-3
       code-4
      }
      

      一旦 'continue' 行被执行,它会直接跳到结束的 "}",忽略 code-3 和 code-4。在这里执行的下一段代码是代码条件。

      【讨论】:

        【解决方案4】:

        在你的情况下没有区别。

        但是 continue 将立即移动到循环的下一次迭代,如果有任何代码,则跳过它之后的任何代码。

        考虑一下:

        int x;
        for(x = 0; x < 100; x++){
            if(x == 50){
                //do something special
                continue;
            }
            //code here is skipped for 50
            //do something different for every other number
        }
        

        因此 continue 语句在某些情况下可能很有用,但对于您的代码而言,它绝对没有任何区别(可能取决于编译器,但它只会增加最终可执行文件的大小,添加另一个 jmp 指令,或者也许不会,因为它可能会完全展开循环)。

        【讨论】:

          【解决方案5】:

          continue 语句在您的示例中没有用。它应该用于将代码执行移动到循环的末尾:

          while (is_true)
            {
              ...
          
              if (foo)
                continue;
          
              if (bar)
                break;
          
              ...
          
              /* 'continue' makes code jump to here and continues executing from here */
            }
          /* 'break' makes code jump to here and continues executing from here */
          

          您可以将continue 视为“评估循环条件并在条件为假时继续循环”,您可以将break 视为“退出循环,忽略循环条件”。

          do-while 相同:

          do
            {
              ...
          
              if (foo)
                continue;
          
              if (bar)
                break;
          
              ...
          
              /* 'continue' moves execution to here */
            }
          while (is_true);
          /* 'break' moves execution to here */
          

          【讨论】:

            【解决方案6】:

            我会简化块

                for (i = 3; i < x; i += 2){
                    if (x % i == 0){
                        return 0;
                        break;
                    }else {
                        continue;
                    }
                }
            

                for (i = 3; i < x; i += 2){
                    if (x % i == 0){
                        return 0;
                    }
                }
            

            额外的行不会改变代码的行为。

            【讨论】:

            • @iKiWiXz,现在已修复 :)
            猜你喜欢
            • 1970-01-01
            • 2016-04-16
            • 1970-01-01
            • 2013-08-11
            • 1970-01-01
            • 1970-01-01
            • 2014-08-31
            • 2011-03-24
            • 2011-03-17
            相关资源
            最近更新 更多