Very quick, easy question

This is a discussion on Very quick, easy question within the C++ Programming forums, part of the General Programming Boards category; What does x++ mean exactly? The effect it has seems to be to add one to x, but I don't ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    4

    Very quick, easy question

    What does x++ mean exactly? The effect it has seems to be to add one to x, but I don't know if that's its only use.
    Code:
    for ( int x = 0; x < 10; x++  ) {
    etc...

  2. #2
    Registered User
    Join Date
    Jun 2005
    Location
    Sarajevo, Bosnia and Herzegovina
    Posts
    18
    That is same like x = x +1

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    4
    So "++" is exactly the same as "+ 1" in all circumstances?

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    No, it isn't. For example, take the following statements:
    Code:
    int x = 1;
    int a = (x = x + 1);
    and
    Code:
    int x = 1;
    int b = x++;
    In both cases, x will be 2 at the end of exection, but in the first example, a will have a value of 2, because it evaluates (x=x+1) first, and then assigns the value of x to a. In the second example, b will have the value of 1, because it assigns the value of x to b, and then adds 1 to x.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
                    int i3 = 1;
    	cout << i3++ << endl;
    	cout << i3 << endl;
    	i3 = 1;
    	cout << ++i3 << endl;
    	cout << i3 << endl;
    This is where these make a difference.

  6. #6
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    More concisely(I think).

    x++ does indeed mean x+1, however its the order that matters.

    ------
    Example:

    ++x means, immediately increment x and use it as it was modified

    x++ means, use x now and then increment it

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Talking Just to add to the confusion...

    Code:
    int x = 1;        
    int b = x++;    // b = 1, x = 2   (postfix, increment after assignment)
    int c = ++x;     // c = 3, x = 3   (prefix,  increment before assignment)

    Code:
    int x = 10;        
    int b = x--;    // b = 10, x = 9   (postfix, decrement after assignment)
    int c = --x;     // c = 8, x = 8   (prefix,  decrement before assignment)

  8. #8
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    x++ is equivalent to something, it's just that it's

    Code:
    x++     <->     (x = x + 1, x - 1)
    ++x     <->     (x = x + 1)
    x--     <->     (x = x - 1, x + 1)
    --x     <->     (x = x - 1)
    Note that the comma operator evaluates the expression on the left, then evaluates the expression on the right, and returns that value.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So "++" is exactly the same as "+ 1" in all circumstances?
    x++ and ++x have the same effect as x = x + 1...until you put them in a more complex expression. Once you do that, you can read ++x and x++ as "Oh my god! I just had demons fly out my nose!". That should protect you until you know better.
    My best code is written with the delete key.

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >"Oh my god! I just had demons fly out my nose!"
    Thanks a lot, Prelude. Milk just flew outta my nose... *cries*
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    I have yet to proclaim "Oh my god! I just had demons fly out my nose!" I guess I am not pre and post incrementing correctly then .

    Anyway as for the OP I think he has enough answers to repel those demons from flying out his nose for the time being.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 09:48 PM
  2. Quick Easy Question
    By St0rmTroop3er in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-24-2004, 01:08 AM
  3. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 02:54 AM
  4. * quick question *
    By SavesTheDay in forum C Programming
    Replies: 3
    Last Post: 03-27-2002, 06:58 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

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