Fundamentals 5 min read

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.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Are while(1) and for(;;) Really Different? A Deep Dive into Infinite Loops

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,"",@progbits

for.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,"",@progbits

The 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.

while loop diagram
while loop diagram
for loop diagram
for loop diagram

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

C++code optimizationAssemblyfor loopwhile-loopinfinite loop
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

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.