問(wèn)題描述
可能的重復(fù):
for 和 while 循環(huán)中沒(méi)有循環(huán)條件
為什么允許 for 循環(huán)
中的條件為空?例如
Why is the condition in a for-loop
allowed to be left empty? For example
for (;;)
編譯正常.為什么這個(gè)空表達(dá)式的計(jì)算結(jié)果為 true
而下面的
compiles fine. Why does this empty expression evaluate to true
but the following
if () {}
while () {}
兩者都會(huì)失敗嗎?我想知道是否/為什么for循環(huán)
是這種情況的一個(gè)例外.
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
這些是迭代語(yǔ)句
while(表達(dá)式)語(yǔ)句
while ( expression ) statement
do 語(yǔ)句 while ( 表達(dá)式 ) ;
do statement while ( expression ) ;
for ( 表達(dá)式 [opt] ; 表達(dá)式 [opt] ; 表達(dá)式 [opt] ) 語(yǔ)句
for ( expression [opt] ; expression [opt] ; expression [opt] ) statement
for ( 聲明表達(dá)式 [opt] ; 表達(dá)式 [opt] ) 語(yǔ)句
for ( declaration expression [opt] ; expression [opt] ) statement
while 循環(huán)旨在評(píng)估控制表達(dá)式before 每次執(zhí)行循環(huán),而 do 循環(huán)旨在評(píng)估after 每次執(zhí)行.
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 循環(huán)被設(shè)計(jì)為更復(fù)雜的迭代語(yǔ)句.
The for loop was designed as a more sophisticated iteration statement.
6.8.5.3 for 語(yǔ)句
6.8.5.3 The for statement
聲明
for ( clause-1 ; expression-2 ; expression-3 ) 語(yǔ)句
for ( clause-1 ; expression-2 ; expression-3 ) statement
行為如下:表達(dá)式 expression-2 是控制表達(dá)式,即在每次執(zhí)行循環(huán)體之前評(píng)估.表達(dá)方式每次執(zhí)行后,表達(dá)式 3 被評(píng)估為一個(gè)空表達(dá)式循環(huán)體.如果子句 1 是聲明,則任何它聲明的標(biāo)識(shí)符是聲明的其余部分,而整個(gè)循環(huán),包括其他兩個(gè)表達(dá)式;它是在首次評(píng)估控制之前的執(zhí)行順序表達(dá).如果子句 1 是表達(dá)式,則將其計(jì)算為 void控制表達(dá)式的第一次計(jì)算之前的表達(dá)式.
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 和表達(dá)式 3 都可以省略.一個(gè)省略表達(dá)式 2 被一個(gè)非零常量替換.
Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.
規(guī)范允許表達(dá)式 2(循環(huán)的條件)被省略,并由一個(gè)非零常量代替.這意味著 for 循環(huán)將無(wú)限期地繼續(xù)執(zhí)行.
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.
這對(duì)于允許無(wú)止境迭代的簡(jiǎn)單語(yǔ)法很有用.
This is useful for allowing a simple syntax for iterating with no end.
for(int i = 0;;i++) { //do stuff with i }
這比編寫一個(gè)在循環(huán)外聲明變量然后在循環(huán)內(nèi)遞增的 while(1) 循環(huán)要簡(jiǎn)單得多.
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 循環(huán)規(guī)范繼續(xù)允許您省略子句 1,以便您可以在其他地方聲明或初始化變量,并且您可以省略表達(dá)式 3,以便您不需要在每個(gè)循環(huán)完成時(shí)評(píng)估任何表達(dá)式.
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 循環(huán)是一個(gè)特例.while 循環(huán)和 do 循環(huán)是嚴(yán)格的,需要一個(gè)表達(dá)式,而 for 循環(huán)是一個(gè)靈活的迭代語(yǔ)句.
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.
這篇關(guān)于為什么for循環(huán)的條件可以留空?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!