Question concerning for loops

This is a discussion on Question concerning for loops within the C++ Programming forums, part of the General Programming Boards category; In a for loop, does the variable update occur first, or checking the condition? Also, does the answer to that ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Question concerning for loops

    In a for loop, does the variable update occur first, or checking the condition? Also, does the answer to that question always hold true...i.e. even when the loop is being executed the first time? Or is the condition checked the first time, then the variable is updated, and then when it reaches the END brace, it jumps back to the beginning of the for loop, updates the variable, and then checks the condition, and decides whether or not to run the code again, or skip it?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The condition is checked at the top of the loop (even before it executes the first time), and the update is always at the bottom of the loop.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question

    Quote Originally Posted by tabstop View Post
    The condition is checked at the top of the loop (even before it executes the first time), and the update is always at the bottom of the loop.
    Thanks, but I'm not exactly sure what you mean by "and the update is always at the bottom of the loop." In a FOR loop, the update is at the top of the loop (at least in physical order of the code). So one would think, the update would occur close to the same time the condition is checked, though my question was, does it occur after checking the condition, or before, and if its the same even the first time the loop is executed.

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, so I bet I can guess how it works.

    1. The condition is checked, and if it is met, it executes the code inside the loop, without updating the variable.
    2. When it reaches the END brace at the end of the loop, it jumps back to the beginning of the loop, updates the variable, and THEN checks the condition, and decides whether or not to run it again.

    Am I correct?

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    The update occurs after the body of the for loop is executed. It almost functions as if it were the last line in the loop's body.

    It is part of the for parens at the beginning because it makes it easy to understand what the loop loops through.
    Last edited by King Mir; 05-26-2009 at 01:50 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    Quote Originally Posted by Programmer_P View Post
    Ok, so I bet I can guess how it works.

    1. The condition is checked, and if it is met, it executes the code inside the loop, without updating the variable.
    2. When it reaches the END brace at the end of the loop, it jumps back to the beginning of the loop, updates the variable, and THEN checks the condition, and decides whether or not to run it again.

    Am I correct?
    Yep.

    Excepting behavior due to keywords break and continue.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, thanks.
    That's what I was figuring was the case. I just needed confirmation.
    So, in the following code:
    Code:
    for ( i = 0; i < 10; ++i )
        {
            array[ i ] = 10 - i;
        }
    the loop body will only be executed once, because "i" is given the initial value of 0, and than it is assigned the value of 10 - 0, which is 10, so "i" becomes 10, and then is incremented by 1, so it becomes 11, therefore breaking the condition's rules, and so consequently, it exits the loop.
    Ok, that makes sense.
    That's the only way it would work, because otherwise the loop would continue forever, updating "i" from 0 to 1, and then from 1 to 9, and then back to 0 again, to continue the never-ending cycle. Of course, that would only hold true though if "i = 0" (i.e. the variable initialization in the FOR loop) was read every single time.
    I'm assuming the variable initialization only happens once?
    Otherwise, based on what you just confirmed was true, "i" would start off as 0, become 10, then 11, and then become 0 again, and the loop would continue forever, just like in the previous example where "i" would be updated the first time.
    Last edited by Programmer_P; 05-26-2009 at 02:10 PM.

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Programmer_P View Post
    Ok, thanks.
    That's what I was figuring was the case. I just needed confirmation.
    So, in the following code:
    Code:
    for ( i = 0; i < 10; ++i )
        {
            array[ i ] = 10 - i;
        }
    the loop body will only be executed once, because "i" is given the initial value of 0, and than it is assigned the value of 10 - 0, which is 10, so "i" becomes 10, and then is incremented by 1, so it becomes 11, therefore breaking the condition's rules, and so consequently, it exits the loop.
    lol, not even close

    Code:
    i = 0;
    while(cond) {
      stuff();
      i++;
    }
    is equivalent to

    Code:
    for(i = 0; cond; i++) {
      stuff();
    }
    iirc my instructor said the two even generate the same machine code
    Last edited by ಠ_ಠ; 05-26-2009 at 02:17 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question

    Quote Originally Posted by ಠ_ಠ View Post
    lol, not even close
    How is that not even close? The "i" variable starts off as 0, becomes 10, and then 11 (because of the variable update), and breaks the condition.
    I edited my last post, in case you hadn't noticed. Realized what was missing in my logic, i.e. the variable update, and added it, so the only thing that I had wrong in the part you quoted, at least, was I forgot 10 would become 11. But, the same logic applies though, and the loop body will definitely only be executed once.
    At least, if what I was just told has any basis in fact and truth...

  10. #10
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Programmer_P View Post
    How is that not even close? The "i" variable starts off as 0, becomes 10, and then 11 (because of the variable update), and breaks the condition.
    I edited my last post, in case you hadn't noticed. Realized what was missing in my logic, i.e. the variable update, and added it, so the only thing that I had wrong in the part you quoted, at least, was I forgot 10 would become 11. But, the same logic applies though, and the loop body will definitely only be executed once.
    At least, if what I was just told has any basis in fact and truth...
    the body of the loop doesn't change i, it changes array[i]
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  11. #11
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by ಠ_ಠ View Post
    the body of the loop doesn't change i, it changes array[i]
    No, "i" is an element of the array, and since it has the same variable name as the variable used in the loop's stuff, its placing the "i" variable, I think, into the array. "i" is given the original value of 0 (i.e. the first element of the array), which is then defined as 10 - i (i.e. 10 - 0 because "i" equals 0). And so, "i" becomes 10, and then is updated to 11 by the variable update section of the loop, which breaks the condition, so it exits the loop.
    Last edited by Programmer_P; 05-26-2009 at 02:28 PM.

  12. #12
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Programmer_P View Post
    No, "i" is an element of the array, and since it has the same variable name as the variable used in the loop's stuff, its placing the "i" variable, I think, into the array. "i" is given the original value of 0 (i.e. the first element of the array), which is then defined as 10 - i (i.e. 10 - 0 because "i" equals 0). And so, "i" becomes 10, and then is updated to 11 by the variable update section of the loop, which breaks the condition, so it exits the loop.
    hahaha no
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Lightbulb

    Here's the whole int main() function, which includes the loop.

    Code:
    int main()
    {
        int array[10];
        int i;
        /* fill array */
        for ( i = 0; i < 10; ++i )
        {
            array[ i ] = 10 - i;
        }
        qsort( array, 10 , sizeof( int ), int_sorter );
        for ( i = 0; i < 10; ++i )
        {
            printf ( "%d\n" ,array[ i ] );
        }
    
    }

  14. #14
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    you forgot return 0;
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  15. #15
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Wink

    That's not MY code!
    It can be found in the following tutorial on this site:

    Function Pointers in C and C++ - Cprogramming.com

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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