問題描述
可能的重復:
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 identi?ers 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 ?rst evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the ?rst 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循環的條件可以留空?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!