可能重複:
Is 「for(;;)」 faster than 「while (TRUE)」? If not, why do people use it?爲什麼選擇(;;){} while while(1)?
我看到這個
for (;;)
{
// Some code here
}
經常。但它提供了什麼好處,爲什麼不選擇while(1){}
?
可能重複:
Is 「for(;;)」 faster than 「while (TRUE)」? If not, why do people use it?爲什麼選擇(;;){} while while(1)?
我看到這個
for (;;)
{
// Some code here
}
經常。但它提供了什麼好處,爲什麼不選擇while(1){}
?
它們產生相同的代碼。有幾個原因可能會更喜歡for (;;)
原因,但它是所有隻是個人喜好:
一些編譯器會警告你總是爲真條件。 for(;;)
不會有這個問題。
for (;;)
逐字讀作「永遠只是循環!」,而while (true)
仍然出現有某種條件。
我說挑一個,堅持下去。只要不擅自在它們之間切換,這並不重要。
個人觀點,但'for(;;)'從字面上看是壞的形式,而'while(真)'的字面意思是「Just loop forever!」它們在編譯器生成的行爲和彙編代碼中完全相同。 –
@DavidHammen準確地說!我只是給出了「for(;;)」可能更常見的原因。 –
這是的形式永遠循環,Kernighan and Ritchie used in their book*。絕對沒有其他理由更喜歡一種形式。
*第3.5節的While和For循環中,示例四。
小1個字。 [Go](http://golang.org/doc/effective_go.html#for)甚至沒有while循環,所以Go程序員更容易理解? – tjameson
這並不重要 - 它只是個人偏好。
我喜歡for(;;)
更好,因爲我認爲它強調永遠循環方面。
我認爲'while(true)'看起來像是一個永遠是'真'的條件。 :-) –
好的。我搜索了它。但谷歌和Stackoverflow搜索功能都沒有返回足夠的結果。 – ManuelSchneid3r