Thread: incrementation inside for loop

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    16

    incrementation inside for loop

    Code:
    int i, j = 0;
    for (i = 0; i++ < 10; ++i, j++);
    j += i;
    printf("%d %d", i, j);
    The output is
    Code:
    11 16
    . I've tried modifying the code to
    Code:
    int i, j = 0;
    for (i = 0; i++ < 10; ++i, j++);
    {
          j += i;
          printf("%d %d", i, j);
    }
    and the output is still the same. Does this imply the for loop is executed only once? Can anyone explain to me how the above code works please, thanks!

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    It's the same because you follow both for() headers with ; -which is basically a for loop with a single NULL statement, it does nothing during the loop.

    So in both cases, the j+= i; and printf() statements don't happen until the loop exits.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Code:
    int i, j = 0;
    for (i = 0; i++ < 10; ++i, j++);
    j += i;
    printf("%d %d", i, j);
    Line 2 executes 5 times, but because of the semicolon, the loop only executes the i++, ++i, and j++ expressions. After 5 iterations, i is 11 and j is 5, and the loop terminates.

    Line 3 then adds i to j giving 11 and 16, and line 4 prints.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    16
    Ah I see, thank you all!
    So in the first iteration, the original value of i(0) is used to do the comparison, after that it's incremented by 1, and since the condition is true it's incremented by 1 again?

    I've also tried changing the position of '++' in the for loop like for(i = 0; ++i < 10; ++i, j++); or for(i = 0; i++ < 10; i++, j++); and still get the same output as before. Why is that so?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    for(i = 0; ++i < 10; ++i, j++);
    You only need the semicolon if the loop is not supposed to have a body. Even then, you should put the semicolon on the next line, so people notice it.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by drawar View Post
    Ah I see, thank you all!
    So in the first iteration, the original value of i(0) is used to do the comparison, after that it's incremented by 1, and since the condition is true it's incremented by 1 again?

    I've also tried changing the position of '++' in the for loop like for(i = 0; ++i < 10; ++i, j++); or for(i = 0; i++ < 10; i++, j++); and still get the same output as before. Why is that so?
    There actually is a slight difference internally, but the output is the same.


    for(i = 0; ++i < 10; ++i, j++);
    for(i = 0; i++ < 10; i++, j++);

    The expressions in blue are equivalent - the only time that i++ vs. ++i matters is if the result is being used in another expression, and these are not.

    The expressions in red actually are different, but they produce the same final result because each iteration increments twice:

    With ++i, you make the following comparisons:
    1 < 10 - True
    3 < 10 - True
    5 < 10 - True
    7 < 10 - True
    9 < 10 - True
    11 < 10 - False

    With i++ you make the following:
    0 < 10 - True
    2 < 10 - True
    4 < 10 - True
    6 < 10 - True
    8 < 10 - True
    10 < 10 - False

    In both cases, the comparison happens six times and the inner body of the loop happens five times, which is why i is incremented 11 times. You WOULD notice a difference if the number being compared to was odd, not even, or if you used a <= rather than a <
    Last edited by Cat; 02-24-2013 at 12:17 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  2. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  3. HEX-numbers after incrementation x in for-loop.
    By Jelte in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2009, 10:36 AM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM