what is the order of FOR command

This is a discussion on what is the order of FOR command within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    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. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    How about adding a printf statement to print out the value of k every loop and find out for yourself?

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so when the loop ends the value of K is 10
    ?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How about adding a printf statement to print out the value of k every loop and find out for yourself?

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Quote Originally Posted by Adak View Post
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.
    My best code is written with the delete key.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    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?
    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

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    No, it's not.
    It's probably the best way of understanding things.
    Printf-crap & co is overkill.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  13. #13
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by Prelude View Post
    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. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    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

    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-29-2008, 12:26 AM
  2. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 04:45 PM
  3. Memory Order in Classes
    By skewray in forum C++ Programming
    Replies: 13
    Last Post: 12-14-2006, 06:40 AM
  4. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 06:26 PM
  5. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21