Are while(1) and for(;;) Really Different? A Deep Dive into Infinite Loops
While both while(1) and for(;;) create infinite loops in C, this article explains their syntax, execution flow, and demonstrates through compiled assembly that, despite subtle differences, they generate identical machine code, helping developers understand any performance or size implications.
Difference between while(1) and for(;;)
Both constructs implement an infinite loop in C, but their syntax and compilation details differ slightly.
Syntax
while syntax:
while (expression) {
statement
}In an infinite loop the expression is the constant 1, which is always true.
for syntax:
for (expr1; expr2; expr3) {
statement
}When all three expressions are omitted, the loop becomes for(;;), which the compiler usually optimizes directly into an endless jump.
Execution process
While loop evaluates the condition each iteration; for loop evaluates init, condition, and increment steps as described.
Empirical comparison
Two source files were compiled with GCC to assembly:
while.c
// filename: while.c
int main(int argc, char const *argv[])
{
while (1) { }
return 0;
}for.c
// filename: for.c
int main(int argc, char const *argv[])
{
for (;;) { }
return 0;
}Assembly generated for both files is essentially identical apart from the file name comment:
while.s
; filename: while.c
.file "while.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
.L2:
jmp .L2
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 9.3.0"
.section .note.GNU-stack,"",@progbitsfor.s
; filename: for.c
.file "for.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
.L2:
jmp .L2
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 9.3.0"
.section .note.GNU-stack,"",@progbitsThe only difference is the filename comment; the generated machine code is the same, confirming that both loops compile to identical instructions under the same compiler and optimization settings.
Images illustrate the control‑flow diagrams for each loop.
Conclusion: while(1) and for(;;) are semantically equivalent for creating infinite loops, and in typical compilation they produce identical assembly, so any perceived performance difference is negligible.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
