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
??
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
??
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
How about adding a printf statement to print out the value of k every loop and find out for yourself?
so when the loop ends the value of K is 10
?
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.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
How about adding a printf statement to print out the value of k every loop and find out for yourself?
thanks
It might help to compare your loop with an equivalent while loop:
The for construct can be confusing at first.Code:/* for(k=0;k<10;k++) */ k = 0; while ( k < 10 ) { /* Body of the loop */ k++; }
My best code is written with the delete key.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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).
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
puts("You made it this far...");
which perhaps demonstrates that I could really use a poll of other people's opinions to help me consider more fulfilling alternatives
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge