Public Group

#### Archived

This topic is now archived and is closed to further replies.

# for loop statements

This topic is 5421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Every C programmer understands how to *use* a for-loop, but I was wondering if anybody could break down the pragmatics to how it works. for(init. statement; test condition; update {} for() is not a function, but what the hell is it? Like the sizeof() operator or return().....macros? If so, how does one make his/her own macro? But this is getting off topic... I''m just curious if anybody knows how the for-loop works under the hood.

##### Share on other sites
Umm..It is just part of the language...I am not sure but I would guess that it is just a while loop:

 for(i=0;i<10;i++){}

is the same as
i=0;while(i<10){//codei++;}

##### Share on other sites
As far as my knowledge goes (which, if you were to measure it, you might need a microscope), loops, including for, while, do while, etc, as well as statements such as if and switch, are all part of the C/C++ syntax, and I don''t think you can make your own loop, except using the ones already given. That''s like trying to create your own thing that works like #include or something.
I''m not really sure if I answered your question, but gimme a break! I''m half asleep!
Hehe.

##### Share on other sites
If you want to know what these statements translate into, you can always look at the assembly output (there is an option in most compilers to generate the intermediate asm code). Exactly what you get depends on the compiler and instruction set you compile for. You will notice that most loops, whether for, while, do while, or if goto, compile to very similar code, with the difference being mainly the order and presence or abscense of certain steps.

##### Share on other sites
ShaneD-

As with all things C and C++ they are converted down into assembly code, some lines of C code become a single line of assembly code, most become quite a few.

Have a look at this link, it shows how a for loop would be generated in assembly.

Doolwind

---------------------------------------------------------
Who of you by worrying can add a single hour to his life.
Matthew 6:27

##### Share on other sites
these things are language keywords, which are basically macros or functions that the compiler will use to expand or genereate code in a certain way.
keywords give a special structure to the language so that certain coding styles can be used, and so that many common operations are simplified for the programmer.

simplified common operations would be the loops, if/swicth statements, and variable declarations, while structural things are like classes, structs, namespaces, function syntax, etc.

##### Share on other sites
The for loop works like it sounds... in ASM there is basically only one kind of loop... and that''s what they all end up as...

initialize stuff
BEGIN:
check condition... if false.. goto END
do whatever your for loop does
do increment step
GOTO BEGIN
END:

looking at that.. it seems a bit dodgy... I''ll explain a little better... in ASM, you have commands which jump (like goto) based on the condition codes. When you do any form of comparison (or certail operations, eg decrementing ecx) the condition codes are set. If you don''t meet the jump criterion they continue executing in the current scope. The above version I wrote is loke a for loop... note that it checks the exit condition initially so it can run 0 times. A do loop, has this test at the end (instead of the goto.. it''s a conditional jump) and no increment step. A while loop is just like a for loop, but the increment step is not implicit.

Erm, I think I''ve made that all more confusing that it needs to be, but I hope it helps.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013452
×