为什么(?;)循环的行为像无限循环? [英] Why do for(;;) loops behave like infinite loops?

查看:151
本文介绍了为什么(?;)循环的行为像无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近一个关于 for(;;){} loops的问题的答案()似乎没有回答我的一些东西,所以我想我会尽量改进这个问题。特别是,除了知道 for 没有条件的循环是无限循环,我想知道为什么它们是无限循环。



在<; $ c $>的语句中(; _;){} _ 是一个条件表达式。我的第一个猜测是,一个空的表达式可能会计算为 0 NULL 。但是如果你测试:

pre $ for(;;){}

是一个无限循环,正如每个人都指出的那样。 for(; 1;){}

是一个无限循环。

但这两个循环体都不会执行:

$ $ $ $ $ $ $ $ $ $
for(; NULL;){}

因此,空的条件表达式似乎没有评估为 0 NULL



我的问题是:(;;){} 循环的行为是C评估表达式的方式,还是只是一个特殊的实现定义的情况,因为一个永远不会执行的循环体是不是非常有用?

更新:
阅读完评论和回答后,我意识到我的问题不是很清楚按照可能的方式制定。我想这个问题是双重的:
$ b $ ol

  • 对于(;;) {} 循环严格地说是C评估表达式的结果,或者是C针对语句评估的方式所特有的行为。事实证明,这些替代方案中的第二个就是这种情况,经过进一步的反思,这才有意义。通常,缺少的表达式会触发编译器错误。例如, while(){} 不会被编译。

  • code> for 循环缺少条件表达式吗?我原来建议,对于循环来说,对于一个永远不会执行的主体来说,并不是很有用。但是在我看来,如果你错误地把条件从你的中的语句之外,在标准规则下,你的程序可能会崩溃到无限循环。这可能是令人惊讶的,但至少你知道它坠毁的地方。但是,如果替代行为是规则的话,那么你的程序就会跳过为的主体,可能会导致一些非常令人讨厌的意外。所以,应用最少惊喜的原则,无限循环行为更为可取。正如@马丁指出的那样,对于循环来说,这仅仅是的一个合理的默认值。

    感谢大家的评论和答复,并帮助我澄清我的问题。

    p> C和C ++都保证这种行为。





    [ C99:6.8.5.3/1]: clause-1 expression-3 都可以省略。一个被省略的表达式-2 由一个非零常量替换。







    [C ++ 14:6.5.3 / 1]: 语句

    $ $ p $ for(for-init-statement conditionopt; expressionopt)语句

    相当于

      {
    -init-statement
    while(condition){
    语句
    表达式;


    [..]

    [C ++ 14:6.5.3 / 2]:条件和表达式可以省略。 (true)
    中缺少的条件使得隐含的 / blockquote>

    The answers to a recent question about for(;;){} loops (What does a for (;;) loop do) did not seem to answer something for me, so I thought that I would try to refine the question a bit. In particular, beyond knowing that for loops without conditionals are infinite loops, I would like to know why they are infinite loops.

    In the statement for (;_;){}, the _ is a conditional expression. My first guess would be that an empty expression might evaluate to 0 or NULL. But if you test:

    for (;;){}
    

    is an infinite loop, as everyone has pointed out.

    for (;1;){}
    

    is an infinite loop.

    But neither of these loop bodies execute at all:

    for (;0;){}
    for (;NULL;){}
    

    Thus, the empty conditional expression does not seem to evaluate to either 0 or NULL.

    So, my question: is the behavior of the for (;;){} loop an artifact of the way that C evaluates expressions, or is it just a special implementation-defined case, because a loop body that never executes is not very useful?

    UPDATE: After reading the comments and answers, I realize that my question wasn't as clearly formulated as it might have been. I suppose that the question was two-fold:

    1. Is the behavior of for(;;){} loops strictly a result of the way that C evaluates expressions in general, or is this behavior specific to the way that C evaluates for statements? It turns out that the second of these alternatives is the case, and upon further reflection this only makes sense. Usually a missing expression triggers a compiler error. For example, while(){} will not compile.

    2. Why was this behavior chosen for for loops lacking conditional expressions? I originally suggested that it is just not very useful to have a for loop with a body that never executes. But it occurs to me that, if you mistakenly leave the conditional out of your for statement, under the rules of the standard, your program will likely crash into an infinite loop. This might be surprising, but at least you know where it crashed. But if the alternative behavior were the rule, your program would skip the body of the for, possibly leading to some very nasty surprises who-knows-where. So, applying the principle of least surprise, the infinite-loop behavior is more desirable. As @martin pointed out, this is just a sensible default for for loops.

    Thanks, everyone, for the comments and answers, and for helping me to clarify my question for myself.

    解决方案

    Both C and C++ guarantee this behaviour.


    [C99: 6.8.5.3/1]: Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.


    [C++14: 6.5.3/1]: The for statement

    for ( for-init-statement conditionopt; expressionopt) statement
    

    is equivalent to

    {
       for-init-statement
       while ( condition ) {
          statement
          expression ;
       }
    }
    

    [..]

    [C++14: 6.5.3/2]: Either or both of the condition and the expression can be omitted. A missing condition makes the implied while clause equivalent to while(true).

    这篇关于为什么(?;)循环的行为像无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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