Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C/C++ Language... are for loop internally compile as While loop? [closed]

Tags:

c++

c

visual-c++

In C/C++ Language... are for loops internally compiled as while loops?


1 Answers

Yes, usually, basically. A for loop, written

for (a; b; c) { ... }

can be rewritten as

a;
while (b) { ...; c; }

Note that this doesn't take into account scoping and such issues, because when you translate a for to a while, scoping can't be written precisely in C++ syntax.

However, underneath, a for loop is really a series of statements and (conditional) gotos:

{
           a;
           goto test;

    body:  
           ...;
           c;

    test:
           if (b) goto body:
}

except that all of it is in assembly code.

like image 71
Seth Carnegie Avatar answered Feb 25 '26 14:02

Seth Carnegie