在和while循环无循环的条件 [英] No loop condition in for and while loop

查看:119
本文介绍了在和while循环无循环的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

while(cond) // fine
for(;cond;) //fine

但是当我删除条件部分

but when I remove the conditional part

while() //syntax compilation error 
for(;;) //Infinite loop

如何这些循环在内部实现?
或者说,如何做编译器(分析器)知道,在空状态,而是误差和为无穷大?

我没有找到这个什么特别,我觉得像我这样的家伙(谁是初学者c)中可能有同样的困惑

I didn't find anything about this particularly, I think guys like me (who are beginner) in C might have same confusion

推荐答案

该标准要求为循环中省略的条件是由一个非零常数代替:

The standard requires that the omitted condition for for loop is replaced by a non-zero constant:

C11 6.8.5.3 :(重点煤矿)

6.8.5.3 for语句

6.8.5.3 The for statement

1语句(第1;前pression-2;前pression-3)
  声明行为如下:这位前pression前pression-2是
  控制所的每一个执行前评估前pression
  循环体。这位前pression前pression-3被评估为一个空白
  循环体的每次执行后的前pression。如果条款-1是一个
  声明,它声明的任何变量的作用域是余
  宣言和整个循环,包括其他两个
  前pressions;它是在执行顺序到达之前的第一
  控制前pression的评价。如果条款-1是
  前pression,它是作为一个空白的前pression前首先评估
  控制前pression.134的评价)

1 The statement for ( clause-1 ; expression-2 ; expression-3 ) statement behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any variables it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.134)

2两个条款-1和前pression-3可以省略。被忽略的前pression-2是由一个非零常数取代。

由于对,而循环没有这样的要求(如果省略的情况),我相信,它留给编译执行。

Since there's no such requirement for while loop (if the condition is omitted), I believe, it's left to the implementation of compiler.

这篇关于在和while循环无循环的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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