# Thread: what is the order of FOR command

1. ## what is the order of FOR command

i got
Code:
`for(k=0;k<10;k++)`

what is the order:
does it checks if k<10 and the does k++
or the opposite
??

2. It does the things in the order you specify them (sort of): initialize, then check condition, then the contents of the loop, then update the iterator, then check the condition and the next iteration of the loop, etc.

Obviously, if the condition is false when it's checked, nothing else is done - the loop ends.

--
Mats

3. How about adding a printf statement to print out the value of k every loop and find out for yourself?

4. so when the loop ends the value of K is 10
?

5. It checks the condition first. That means with your example, the loop will run while k is 0-9, but after the last loop is done, k will equal 10.

6. How about adding a printf statement to print out the value of k every loop and find out for yourself?

7. thanks

How about adding a printf statement to print out the value of k every loop and find out for yourself?
How about using a debugger to find out?

9. It might help to compare your loop with an equivalent while loop:
Code:
```/* for(k=0;k<10;k++) */
k = 0;

while ( k < 10 ) {
/* Body of the loop */
k++;
}```
The for construct can be confusing at first.

10. Originally Posted by Elysia
How about using a debugger to find out?
Isn't that kind of like driving to your neighbour's when you live in row housing?

11. No, it's not.
It's probably the best way of understanding things.
Printf-crap & co is overkill.

12. I personally think both debugger and printf are good tools for this particular problem - either will solve it. One requires changes to the code, the other knowledge of a debugger. Knowing how to use a debugger is really useful!

--
Mats

13. Originally Posted by Prelude
It might help to compare your loop with an equivalent while loop:
Code:
```/* for(k=0;k<10;k++) */
k = 0;

while ( k < 10 ) {
/* Body of the loop */
k++;
}```
The for construct can be confusing at first.

This would be the first logical step for those learning the ropes imho before diving into debugging which leads to more advanced skills.

14. Learning how to use a debugger is the first thing you should do after learning how to compile.
It will save hours of headaches.
And is a super-easy debugger to use for Windows called Visual Studio. It's so easy that pretty much anyone can use it with basic knowledge of debugging (ie, what is step in, step out and step over).

15. Originally Posted by Elysia
No, it's not.
It's probably the best way of understanding things.
Printf-crap & co is overkill.
Wow, I am surprised to read this, but then the only debugger I've tried is gdb.

I was actually thinking of starting a poll (are there polls at cboard?) to find out what people's favourite method of debugging is because mine is definitely

printf

and even