Thread: confused about 'for'

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    confused about 'for'

    Confused about output of following 2 sets of code. Postfix and Prefix notation.

    the way I understood book:

    Using Xcode 3.3 , gcc 4.2


    postfix notation passes on value to i before it increments.

    prefix notation increments before it passes value to i.
    -----------------------------------------------------------
    Postfix notation code
    -----------------------------------------------------------
    for (i = 0; i < 4; i++)

    printf( "first for: i=%d\n", i );

    printf( "After first for loop, i=%d.\n\n", i );
    ------------------------------------------------------------
    Postfix notation code output
    ------------------------------------------------------------
    first for: i=0
    first for: i=1
    first for: i=2
    first for: i=3
    After first for loop, i=4.
    -------------------------------------------------------------
    Prefix notation code
    -------------------------------------------------------------
    for (i = 0; i < 4; ++i)

    printf( "first for: i=%d\n", i );

    printf( "After first for loop, i=%d.\n\n", i );
    -------------------------------------------------------------
    Prefix notation code output
    -------------------------------------------------------------
    first for: i=0
    first for: i=1
    first for: i=2
    first for: i=3
    After first for loop, i=4.

    Why do both sets of code output the same?

    rj
    Last edited by rjreynolds; 07-06-2010 at 07:59 PM. Reason: ommisions

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Here is another way of thinking about how your for loop works:

    Code:
        i = 0;
        while (i < 5) {
            printf("%d\n", i);
            i++;
        }
    Can you see why it would make no difference at all in your code as to whether you use pre or post increment?

    Anyway, if you want to compare the results you were getting with something more like you wanted, look at this code:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i;
        int x = 0;
        int y = 0;
    
        puts("");
    
        for (i = 0; i < 5; i++) {
            printf("%d\n", i);
        }
    
        puts("");
    
        for (i = 0; i < 5; ++i) {
            printf("%d\n", i);
        }
    
    
        puts("");
    
        for (i = 0; i < 5; i++) {
            printf("x = %d\ty = %d\n", x++, ++y);
        }
    
        puts("");
    
        return 0;
    }
    Last edited by kermit; 07-06-2010 at 08:08 PM.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    Starting to clear up. After looking at your example and further reading, I a m starting to understand. It will require further study on my part. I want to make sure I have these concepts well understood before I move on. thanks again for your input....

    rj

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You are definitely on the right track - it is just that how you increment it (either pre or post) only matters in certain contexts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  2. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  3. So Now Im getting confused?!?!?
    By zergdeath1 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2004, 05:41 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM