Post and Prefix Increment Operators

I'm still stumped on this subject. Why do I get different values whenever I use either, and what would I use this for in a practical setting?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>

using namespace std; 

int main () 
{
    int x = 2;
    cout << "x: " << x++ << endl;
    cout << "x: " << ++x << endl;
    
    system ("pause");
    
    return 0;
    
};


When x is two, I get the outputs of 2 and 4 respectively. I experimented with other integers and they were all two larger for the prefix increment. Will this be true all the time ? When would it be beneficial? Why shouldn't I just add two to x ?
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <limits>

int main () 
{
    int x = 2;
    std::cout << "x: " <<   x++ << std::endl;
    std::cout << "x: " <<   x   << std::endl;
    std::cout << "x: " << ++x   << std::endl;

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
http://ideone.com/pwWmwW

Postfix notation will increment x, but return the original value as it was before incerementing, generally by making a copy before it increments and returning the copy. In 99% of cases, you would use prefix notation, but there are a few rare cases where it is more preferable to use postfix.
Last edited on
@ L B, now I'm even more confused. How did x become 3?
Because x++ made a copy of x, incremented x, and returned the copy. The value of x was changed on line 7.
@ L B so every X following that line will always be valued as 3 until changed again?
I experimented again. Switched lines 7 and 9 and all the "x"s ended up being 3's.
Last edited on
nsahawks7 wrote:
so every X following that line will always be valued as 3 until changed again?
Yes.
And when would this be practical? Sorry about all the questions, but I really wanna get a good understanding of this.
When would what be practical? You need to be more specific.
Using these increments. x++ or ++x.
The increment operators are syntactic sugar; they make it a bit simpler (and possibly avoid a typo or two).

You could write them open:
1
2
3
4
5
6
7
8
9
10
// int a = ...;
// int b = 0;

// pre
a = a + 1;
b = a;

// post
b = a;
a = a + 1;

The standard library reference documentation has examples of what the standard algorithms are equivalent of, such as this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
...
if (*first1<*first2)
{
  *result = *first1;
  ++result;
  ++first1;
}
else if (*first2<*first1)
{
  ++first2;
}
else
{
  ++first1;
  ++first2;
}
...

That would be longer and no more clear, if unwound.

Prefer to use the pre-[inc|dec]rement. You will learn when you absolutely need the post-version.
Here is what I got from everything you guys told me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
//testing out pre and post incrememnts

int main () {
    
    int x =2;
    
    //post increment used here
    std::cout << "x: " << x++ << std::endl;
    //the x value is incremented AFTER
    std::cout << "x: " << x << std::endl;
    
    int y=2;
    
    //pre increment used here
    std::cout << "y: " << ++y << std::endl;
    //the y value is incremented BEFORE and stored
    std::cout << "y: " << y << std::endl;
    
    int z = 2;
    
    //mutliplying with post.
    //the z value is multiplyed by 5 before being incremented, making the output 10
    std:: cout << "z: " << z++ * 5 << std::endl;
    // since z has been incremented, it will show up as 3
    std::cout << "z: " << z << std:: endl;
    
    int a = 2;
    
    //a will be incremented first, then multiplyed by five, so the output will be 15
    std:: cout << "a: " << ++a * 5 << std::endl;
    //like z, a will be 3 because the value has been incremented already
    std::cout << "a: " << a << std:: endl;
    
    system ("pause");
    
    return 0;
    
}



"Prefer to use the pre-[inc|dec]rement. You will learn when you absolutely need the post-version."

---any reason why?
Last edited on
It's all a bout precedence say for example you want a variable to increase by one each time then multiply it by two you would want it to increase by one before multiplying by two not after.

1
2
3
4
int x = 1;
int y = 0;

y = ++x * 2;


Though technically you should probably put
1
2
++x;
y = x * 2



Check out these links:
http://www.learncpp.com/cpp-tutorial/31-precedence-and-associativity/
http://www.learncpp.com/cpp-tutorial/33-incrementdecrement-operators-and-side-effects/
http://www.cplusplus.com/doc/tutorial/operators/

Increase and decrease (++, --)
Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:

1
2
3
++c;
c+=1;
c=c+1;


are all equivalent in its functionality: the three of them increase by one the value of c.

In the early C compilers, the three previous expressions probably produced different executable code depending on which one was used. Nowadays, this type of code optimization is generally done automatically by the compiler, thus the three expressions should produce exactly the same executable code.

A characteristic of this operator is that it can be used both as a prefix and as a suffix. That means that it can be written either before the variable identifier (++a) or after it (a++). Although in simple expressions like a++ or ++a both have exactly the same meaning, in other expressions in which the result of the increase or decrease operation is evaluated as a value in an outer expression they may have an important difference in their meaning: In the case that the increase operator is used as a prefix (++a) the value is increased before the result of the expression is evaluated and therefore the increased value is considered in the outer expression; in case that it is used as a suffix (a++) the value stored in a is increased after being evaluated and therefore the value stored before the increase operation is evaluated in the outer expression. Notice the difference:

Example 1 Example 2
B=3;
A=++B;
// A contains 4, B contains 4 B=3;
A=B++;
// A contains 3, B contains 4

In Example 1, B is increased before its value is copied to A. While in Example 2, the value of B is copied to A and then B is increased.


@giblit operator precedence is not directly related to this topic...
How is it not?
http://ideone.com/Rn7rkG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
 
int main() {
	int x = 1;
	int y = 1;
	int z = 0;
	z = x++ + y++; //z = x + y = 1 + 1 = 2 , x = 2 , y = 2
	std::cout << z << std::endl;
	x = 1;
	y = 1;
	z = ++x + ++y; //x = 2, y = 2 , z = x + y = 2 + 2 = 4
	std::cout << z << std::endl;
	x = 1;
	y = 1;
	z = ++x + y++; //x = 2 , z = x + y = 2 + 1 = 3 , y = 2;
	std::cout << z << std::endl;
	return 0;
}

Last edited on
Nowhere in the topic until just now did we write or discuss any code which involved operator precedence (unless you're counting the formatted output operator for std::cout).
Topic archived. No new replies allowed.