为什么for循环的条件可以留空?

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

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

问题描述

可能的重复:
for 和 while 循环中没有循环条件

为什么允许 for 循环 中的条件为空?例如

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

for (;;)

编译正常.为什么这个空表达式的计算结果为 true 而下面的

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

if () {}
while () {}

两者都会失败吗?我想知道是否/为什么for循环是这种情况的一个例外.

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

这些是迭代语句

while(表达式)语句

while ( expression ) statement

do 语句 while ( 表达式 ) ;

do statement while ( expression ) ;

for ( 表达式 [opt] ; 表达式 [opt] ; 表达式 [opt] ) 语句

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

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

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

while 循环旨在评估控制表达式before 每次执行循环,而 do 循环旨在评估after 每次执行.

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.

for 循环被设计为更复杂的迭代语句.

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

6.8.5.3 for 语句

6.8.5.3 The for statement

声明

for ( clause-1 ; expression-2 ; expression-3 ) 语句

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

行为如下:表达式 expression-2 是控制表达式,即在每次执行循环体之前评估.表达方式每次执行后,表达式 3 被评估为一个空表达式循环体.如果子句 1 是声明,则任何它声明的标识符是声明的其余部分,而整个循环,包括其他两个表达式;它是在首次评估控制之前的执行顺序表达.如果子句 1 是表达式,则将其计算为 void控制表达式的第一次计算之前的表达式.

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.

子句 1 和表达式 3 都可以省略.一个省略表达式 2 被一个非零常量替换.

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

规范允许表达式 2(循环的条件)被省略,并由一个非零常量代替.这意味着 for 循环将无限期地继续执行.

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 }

这比编写一个在循环外声明变量然后在循环内递增的 while(1) 循环要简单得多.

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.

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

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.

for 循环是一个特例.while 循环和 do 循环是严格的,需要一个表达式,而 for 循环是一个灵活的迭代语句.

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循环的条件可以留空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么for循环的条件可以留空?

基础教程推荐