confusion with increment and decrement operators

This is a discussion on confusion with increment and decrement operators within the C Programming forums, part of the General Programming Boards category; hi..everyone.i got a little bit confusion about prefix form for some book i learn that; for example Code: for (i=0; ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    confusion with increment and decrement operators

    hi..everyone.i got a little bit confusion about prefix form
    for some book i learn that; for example
    Code:
    for (i=0; i < 5; ++i)
    and some told me write in this form
    Code:
    for (i=0; i<5; i++)
    and i know that the output is same but doesn't it
    ++i will increment the value first and if i++ the value will increment after the loop.I really doesn't understand that why the for loop come out with the same answer??...
    Last edited by cBegginer; 03-19-2005 at 12:02 AM.

  2. #2
    Registered User taran's Avatar
    Join Date
    Mar 2005
    Posts
    2
    hi,
    the difference between i++ and ++i is that if the i++ (or ++i) is used in an expression, i++ will cause i to be used in the expression then incremented, wheras ++i will increment i and then use it in the expression. As i isn't being used in an expression in your code, there is no difference between i++ and ++i
    If you want to have i going from 1 to 5 instead of 0 to 4, simply use the following code instead:
    Code:
    for (i=1;i<=5;i++)
    Hope this helps,
    Taran

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    It will be the same because u only use it for loop counter.

    Try this:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i = 0;
    	int j = 0;
    	printf("%d\n", i++);//result 0
    	printf("%d\n", i++);//result 1
    	printf("%d\n", ++j);//result 1
    	printf("%d\n", ++j);//result 2
    	return 0;
    }
    Remember, the difference between the two forms is what value (either the old or the new) is passed on to the surrounding expression. If there is no surrounding expression, if the ++i or i++ appears all by itself, to increment i and do nothing else, you can use either form; it makes no difference

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by cBegginer
    and i know that the output is same but doesn't it ++i will increment the value first and if i++ the value will increment after the loop.I really doesn't understand that why the for loop come out with the same answer??...
    The first form,
    Code:
    for (i=0; i < 5; ++i)
    has an equivalent:
    Code:
       i = 0;
       while(i < 5)
       {
          /* do stuff */
          ++i;
       }
    The second form,
    Code:
    for (i=0; i < 5; i++)
    has an equivalent:
    Code:
       i = 0;
       while(i < 5)
       {
          /* do stuff */
          i++;
       }
    Why would you expect the results to differ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    Ic..ok thanks for helping me.I appreciate it alot.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    ++i and i++ do not really give you the same result everytime.

    ++i in this case is called the pre-increment operator. This will first add 1 to i and give you the new value of i.

    i++ in this case is called the post -decrement operator, because im lacking words to express this i will do it by example.
    Code:
     x =i++;
    here x is assigned the original value of i first and then i is increased by 1.

    Example from the book that im reading.
    Code:
    #include <stdio.h>
    main()
    {
    	int w,x,y,z, result;
    	
    	w = x = y = z = 1; /*initialize x and y*/
    	printf("Given w =%d, x =%d, y=%d, and z =%d,\n", w, x,y,z);
    
    	result = ++w;
    	printf("++w evaluates to %d and w is now %d\n", result, w);
    
    	result = x++;
    	printf("x++ evaluates to %d and x is now %d\n", result, x);
    
    	result = --y;
    	printf("--y evaluates to %d and y is now %d\n", result, y);
    
    	result = z--;
    	printf("z-- evaluates to %d and z is now %d\n", result, z);
    	return 0;
    }
    When no one helps you out. Call google();

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When used alone, ++foo and foo++ give the exact same results:
    Code:
    int foo = 0;
    
    foo++;
    printf( "%", foo );
    Is exactly the same as:
    Code:
    int foo = 0;
    
    ++foo;
    printf( "%", foo );
    In a single expression, the results are the same. The difference is when you use them for something other than a single expression. Like so:
    Code:
    #include<stdio.h>
    void foo( int bar )
    {
        printf("bar is %d\n", bar );
    }
    
    int main( void )
    {
        int baz;
    
        baz = 0;
        foo( baz++ );
    
        baz = 0;
        foo( ++baz );
    
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How Does Increment Operators Work...??
    By ajayd in forum C Programming
    Replies: 37
    Last Post: 12-31-2008, 10:01 AM
  2. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 09:24 PM
  3. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 07:54 AM
  4. Replies: 11
    Last Post: 08-30-2004, 04:56 PM
  5. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 05:57 AM

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