为什么for循环的条件可以为空? [英] Why can the condition of a for-loop be left empty?

查看:3503
本文介绍了为什么for循环的条件可以为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在for和while循环中无循环条件


为什么 for-loop 中的条件允许留空?例如

  for(;;)

编译良好。为什么这个空表达式求值为 true ,但是下面的

  if ){} 
while(){}

我想知道 if / why for-loop 是这种情况的例外情况。

解决方案

http:// www .open-std.org / JTC1 / SC22 / WG14 / www / docs / n1256.pdf



这些都是迭代语句


while(expression)语句



do语句while(expression);



for(expression [opt]; expression [opt]; expression [opt])statement

声明表达式[opt];表达式[opt])语句


while循环用于计算控制表达式每次执行循环和do循环都是为了在之后执行



for循环设计为更多复杂的迭代语句。




6.8.5.3 for语句

/ p>

for(clause-1; expression-2; expression-3)statement



的行为如下:
表达式表达式2是在每次执行循环体之前评估的
的控制表达式。
表达式-3的表达式在每次执行循环体
之后被评估为void表达式。如果子句-1是一个声明,它声明的任何
标识符的范围是声明的剩余部分和
整个循环,包括其他两个表达式;在控制
表达式的第一次评估之前,它在
执行顺序中达到。如果子句-1是一个表达式,它在控制表达式的第一次求值之前被评估为一个void
表达式。



3可以省略。一个省略的
表达式-2被一个非零常数所取代。


规范允许表达式2,循环,将被省略并且被非零常数替代。这意味着for循环将继续无限期地执行。



这对于允许一个简单的语法无限循环是有用的。

  for(int i = 0 ;; i ++){// do stuff with i} 

与写一个while(1)循环一样,在循环外声明一个变量,然后在循环内递增,这样写和理解要简单得多。



然后,for循环规范允许您省略子句-1,以便可以在其他位置声明或初始化变量,并且可以省略expression-3,不需要在每个循环完成时评估任何表达式。



for循环是一种特殊情况。 while循环和do循环是严格的并且需要一个表达式,但是for循环是一个灵活的迭代语句。


Possible Duplicate:
No loop condition in for and while loop

Why is the condition in a for-loop allowed to be left empty? For example

for (;;)

compiles fine. Why does this empty expression evaluate to true but the following

if () {}
while () {}

will both fail? I want to know if/why the for-loop is an exception to this case.

解决方案

http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

These are the iteration statements

while ( expression ) statement

do statement while ( expression ) ;

for ( expression [opt] ; expression [opt] ; expression [opt] ) statement

for ( declaration expression [opt] ; expression [opt] ) statement

The while loop was designed to evaluate the controlling expression before each execution of the loop and the do loop was designed to evaluate after each execution.

The for loop was designed as a more sophisticated iteration statement.

6.8.5.3 The for statement

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

Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

The specification allows expression-2, the condition of the loop, to be omitted and is replaced by a nonzero constant. This means that the for loop will continue to execute indefinitely.

This is useful for allowing a simple syntax for iterating with no end.

for(int i = 0;;i++) { //do stuff with i }

That's much simpler to write and understand than writing a while(1) loop with a variable declared outside the loop and then incremented inside the loop.

The for loop specification then goes on to allow you to omit clause-1 so that you can declare or initialize variables elsewhere, and you can omit expression-3 so that you are not required to evaluate any expression upon completion of each loop.

The for loop is a special case. The while loop and the do loop are strict and require an expression, but the for loop is a flexible iteration statement.

这篇关于为什么for循环的条件可以为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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