Fundamentals 7 min read

Do while(1) and for(;;) Really Differ? A Deep Dive into C Infinite Loops

This article explains the syntax and execution semantics of while(1) and for(;;) in C, compares their generated assembly code, and demonstrates through a practical gcc compilation that both constructs produce identical machine code for an infinite loop.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Do while(1) and for(;;) Really Differ? A Deep Dive into C Infinite Loops

Introduction

When readers ask whether while(1) and for(;;) are truly different infinite loops, the answer lies in their syntax, semantics, and the compiled output. By writing minimal programs with each construct, compiling them with gcc, and inspecting the resulting assembly, we can see the real differences—or lack thereof.

Syntax of the Two Constructs

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 (i.e., for(;;)), the loop has no initialization, condition, or iteration step, and the compiler treats it as an unconditional jump.

Execution Semantics

For while(1) the runtime evaluates the constant expression on each iteration; the constant 1 is compared to zero, which is a trivial true check. For for(;;) the three empty expressions are compiled away, and the loop becomes a direct infinite jump.

Experimental Verification

Two source files were created:

// while.c
int main(int argc, char const *argv[])
{
    while(1) {}
    return 0;
}
// for.c
int main(int argc, char const *argv[])
{
    for(;;) {}
    return 0;
}

Both files were compiled to assembly with the commands:

gcc -S -o while.s while.c
gcc -S -o for.s for.c

The generated assembly for each file is shown below (identical except for the source‑file comment):

; filename: while.s
.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
    jmp .L2
.L2:
    jmp .L2
    .cfi_endproc
    .size main, .-main
    .ident "GCC: (GNU) 9.3.0"
    .section .note.GNU-stack,"",@progbits
; filename: for.s
.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
    jmp .L2
.L2:
    jmp .L2
    .cfi_endproc
    .size main, .-main
    .ident "GCC: (GNU) 9.3.0"
    .section .note.GNU-stack,"",@progbits

Both assemblies contain the same instructions; the only difference is the comment indicating the original source file.

Comparison and Findings

Both loops produce identical machine code for an infinite loop.

The extra constant‑evaluation in while(1) is optimized away by modern compilers, so no extra instructions are emitted.

File size of the generated object files is the same (aside from the source‑file name metadata).

Therefore, in practice there is no performance or size penalty for choosing one form over the other.

Conclusion

The perceived differences between while(1) and for(;;) are only syntactic. Modern compilers generate identical assembly for both, making them interchangeable for infinite loops. The choice can be based on coding style preferences rather than efficiency.

Illustrations

while loop flowchart
while loop flowchart
for loop flowchart
for loop flowchart
compilerC++Assemblycode sizefor loopwhile-loopinfinite loop
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.