Thread: two simple for statements...

  1. #1
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96

    two simple for statements...

    I have a question. How come when I have a for statement and in the middle section, I increment a variable, the variable gets incremented before I go into the for loop? For example,
    Code:
      for (i = 0; i < 10; ++i)
        printf("%d ", i);
      printf("\n");
    
    
      for (i = 0; i++ < 10;)
        printf("%d ", i);
      printf("\n");
    The first for loop is a much more common example, and will print 0 through 9. After each time through the loop, the variable i gets incremented by one, as expected.

    The second one puzzles me a bit though. I understand we don't need all three parameters with the for statement, however, when we execute it, it prints 1 - 10. i is getting incremented before we execute the body of the loop...

    I believe I understand why the loop prints 10 and doesn't exit before then. Correct me if I'm wrong, but the < operator has higher precedence over the ++ operator, right? So first we check if i is greater than 10 and then we increment i, hence the reason we get 10 printed. I just don't understand why i gets incremented before the body gets executed. Is this the way it always is, or is this perhaps compiler specific? For example, with some compilers, it might get incremented after the body gets executed.
    Last edited by Spork Schivago; 09-02-2015 at 09:34 PM. Reason: Clarified question a little and added content

  2. #2
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    In the second for loop, it looks like when i is 9 it is tested and found true, it is less than 10, however after that test it is than incremented to 10 and printed in the printf statement because you used a post increment on the variable. Again, the i is tested, than it is incremented, than the loop is entered.

    That is just my opinion.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by FourAngels View Post
    In the second for loop, it looks like when i is 9 it is tested and found true, it is less than 10, however after that test it is than incremented to 10 and printed in the printf statement because you used a post increment on the variable. Again, the i is tested, than it is incremented, than the loop is entered.

    That is just my opinion.
    You are correct there. I was incorrect with my statement. I tested and using a pre-increment on the variable (++i), it only goes to 9. I know if I use ++i or i++ in the first for loop, it doesn't make a difference. I incorrectly assumed it wouldn't make a difference in the second. Thank you for clearing that up for me.

    However, I still wonder if I increment a variable in the middle parameter of the for statement, like I do in the second for example, will it always increment before entering the loop?

  4. #4
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    You would have to do the update in the for statements body if you wanted the update to happen in a specific place or else use another variable for the counter.

    Code:
    for (int i = 0, int y =0; i++ < 10;) {
       // do something here using y instead of i
       y = i;
    }
    It's my guess.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are mistaken: operator < has a lower precedence than post increment operator++

    The thing to understand is that precedence determines grouping, not order of evaluation. Here, the increment happens in the loop condition, so of course it happens before control enters the loop body. Where pre vs post increment matters is with the comparison, hence post increment means that you're comparing with the value before increment.

  6. #6
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by laserlight View Post
    You are mistaken: operator < has a lower precedence than post increment operator++

    The thing to understand is that precedence determines grouping, not order of evaluation. Here, the increment happens in the loop condition, so of course it happens before control enters the loop body. Where pre vs post increment matters is with the comparison, hence post increment means that you're comparing with the value before increment.
    Okay, that makes sense. I got it, thank you. I think I should try to find or create a list of the operators and their precedence until I can get them memorized. I think that would make things easier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with if statements (simple)
    By aosterminal in forum C Programming
    Replies: 7
    Last Post: 04-25-2012, 09:59 AM
  2. Proper form for Simple Conditional statements?
    By TAZIN in forum C Programming
    Replies: 1
    Last Post: 12-13-2010, 07:42 PM
  3. Simple C Program with Flow Statements
    By rory-uk in forum C Programming
    Replies: 16
    Last Post: 02-06-2008, 01:12 PM
  4. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 08:39 AM
  5. Which one of these if() statements is best?...
    By MathFan in forum C++ Programming
    Replies: 11
    Last Post: 05-07-2005, 02:19 AM