What is the difference between while(1) and for(;;)

This is the 551st technical sharing of “Attack Coder”

Author: strongerHuang

Source: [Embedded] Column

It takes about 5 minutes to read this article.

Some readers have asked questions like this: Aren’t while(1) and for(;;)  both infinite loops, and their functions should be the same, what’s the difference between them?

To answer this question, in fact, you write a while(1) and for(;;) code, compile and compare the code size and assembly file, and you will probably know.

while(1) and for(;;) syntax expressions

Let’s talk about the while(1) and for(;;) syntax expressions first.

1.while syntax expression

while (expression)
{
  statement
}

in:

  • Expression: is the loop condition

  • Statement: for the loop body.

The semantics of the while statement is: evaluate the value of the expression, and when the value is true (not 0), execute the loop body statement. Its execution process can be represented by the following figure:

2.for syntax expression

for (expression1; expression2; expression3)
{
  statement
}

Its execution process is as follows:

  1. Solve Expression 1 first

  2. Solve Expression 2

If its value is true (not 0), execute the inline statement specified in the for statement, and then perform step 3) below;

If its value is false (0), end the loop and go to step 5).

  1. Solve Expression 3

  2. Go back to step 2) above to continue.

  3. The loop ends and a statement below the for statement is executed.

The execution process can be represented by the following diagram:

Similarities and differences between while(1) and for(;;)

The conclusion is first stated here, and then the verification conclusion is verified.

1. The same

The function and effect are the same: it is a function of realizing an infinite loop.

2. Differences

while(1): There is a condition in the parentheses, and the program will judge whether it is true or false. The “1” in parentheses is always a “truth value”.

Among them, each time the loop, the compiler must determine whether the constant 1 is equal to zero.

for(;;): These two ;; empty statements are generally optimized away by the compiler and directly enter an infinite loop.

Based on the above description, you may feel that while(1) does more work than for(;;), has more assembly code, and has a larger code size.

But is this the case? Verify it below.

Verify the difference between while(1) and for(;;)

We write two files, for.c and while.c, respectively, and then generate assembly code respectively to see the situation.

1. Source code

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;
}

2. Generate assembly

Here we use the gcc compiler to generate assembly, and the execution command is as follows:

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

while assembly code:

; filename: whiles
  .file  "while.c"
  .text
  .globl  main
  .type  main, @function
main:
.LFB 0 :
  .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
.LFE 0 :
  .size  main, .-main
  .ident  "GCC: (GNU) 9.3.0"
  .section  .note.GNU-stack,"",@progbits

for assembly code:

; filename: for.s
  .file  "for.c"
  .text
  .globl  main
  .type  main, @function
main:
.LFB 0 :
  .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
.LFE 0 :
  .size  main, .-main
  .ident  "GCC: (GNU) 9.3.0"
  .section  .note.GNU-stack,"",@progbits

You’ll notice that except for the filenames, everything else is the same.

Of course, here’s an additional point, different codes, different compilers, and different optimization levels may have different final results.

End

Cui Qingcai’s new book”Python3 Web Crawler Development (Second Edition)” has been officially launched! The book introduces in detail all aspects of developing crawlers with Python from zero basis. Compared with the first edition, it adds JavaScript reverse, Android reverse, asynchronous crawler, deep learning, and Kubernetes related content. At the same time, this book has won the father of Python. Guido’s recommendation, this book is currently on sale at 30% off!

Content introduction: “Python3 Web Crawler Development Practice (Second Edition)” content introduction

Scan code to buy

Good text to watch with friends~

Leave a Comment

Your email address will not be published. Required fields are marked *