On Zhihu, there is a classic question that has sparked intense discussion.
for(;;)
for infinite loops in demos, instead of the commonly used while(1)
. Is this merely a matter of personal habit, or is there a deeper significance?
No Difference Party: It’s All Psychological
while
needs to evaluate whether the expression in parentheses is non-zero before jumping. However, after careful optimization by the compiler, while(1)
will also be optimized into an unconditional jump (jmp instruction), so there’s no difference from for(;;)
.for(;;)
easily associates with “forever” for native English speakers.while(1)
and for(;;)
do have a syntactical difference: for(;;)
clearly indicates a loop, equivalent to a goto that jumps indefinitely without a comparison condition.while
requires a cmp
operation to set the ZF register before it can perform conditional jumps with jne
and je
. Meanwhile, for(;;)
is an explicit unconditional jump to eip
, without conditional jumps.for(;;)
.#include<stdio.h>int main(){ for(;;) { printf("for\n"); }

#include<stdio.h>int main(){ while(1) { printf("while\n"); }

Proponents’ View: Where Are Good Compilers?
while(1)
would have several more statements than for(;;)
.while
contains a condition, it would translate to:label: …… mov a, #1 jnz label
for(;;)
would generally only be jmp label
.(int)a<<0
, leading to doubts about whether the accompanying chips can handle optimized instructions.
Opponents’ View: This Coding Style is Outdated
for(;;)
to indicate an infinite loop is now considered an outdated style.for(;;)
is a poor style. See:-
GJB 8114-2013 R-1-9-4: Infinite loops must use the
while(1)
statement; usingfor(;;)
and other forms is prohibited. -
CppCoreGuidelines ES.73: Prefer a while-statement to a for-statement when there is no obvious loop variable.
-
360 safe rules: When there is no explicit loop variable in a for statement, it should be replaced with a while statement.
for
statements are specifically used to implement iterative algorithms with a clear number of iterations and loop variables. The three expressions in parentheses should focus on initializing the loop variable, judging the loop condition, and incrementing the loop variable. This creates a clear static structure for the loop, making it easier to read and maintain. If there is no explicit loop variable, a while
loop should be used to avoid misleading the code maintainers.for(;;)
indicates an unconditional loop, while while(1)
requires a conditional check, which is slower than for(;;)
. There is some truth to this, but that was long ago. Even without compiler optimization, this overhead will not become a performance bottleneck, and maintaining a clear static structure of the code is more important!
Engineers Test: Related to Compiler and Optimization
while(1)
logic:
for(;;)
logic:
for(;;)
executes faster (45.863ms) than while(1)
(48.643ms), by about 5.7%.for
are more concise, while those for while
are relatively more complex; in simple terms, for
takes a shortcut, while while
goes around.

while(1)
is simple and clear, while for(;;)
is much more ambiguous. However, for some older dedicated compilers, careful consideration may be needed regarding which form to use.References
[1] Zhihu: https://www.zhihu.com/question/23043337
[2] WKJay: Extreme Optimization, Who is Faster Between while(1) and for(;;)? 2024.1.28. https://mp.weixin.qq.com/s/T21DLqOkqe38XuG3Z3I8SQ
[3] Embedded Column: What is the Difference Between while(1) and for(;;) in Microcontroller Code? 2023.12.1. https://mp.weixin.qq.com/s/mq6sjy1CMPXHIfPpDOt-_A
[4] Embedded Linux: Do You Use while(1) or for(;;) to Write Loop Code? 2021.1.20. https://mp.weixin.qq.com/s/4Y2x2Xm_0rc9_DwdLgYWMA