MJH
MJH

Reputation: 13

C++ Operators pre- and post- incrementing

I am not understanding the pre/post incrementing of this code example:

int1 = 14;
int2 = ++int1;
// Assert: int1 == 15 && int2 == 15

In this pre-incrementing example why does int1 == 15? Why is int1 incremented and not just int2?

Then we have:

int1 = 14;
int2 = int1++;
// Assert: int1 == 15 && int2 == 14

In this post-incrementing example why does int2 == 14? Why is int2 not incremented but int1 is?

Upvotes: 1

Views: 531

Answers (6)

AnT stands with Russia
AnT stands with Russia

Reputation: 320371

Your questions are difficult to answer, since there's no way to figure out how they could even arise.

In your first question you are asking why int1 is incremented. The only possible answer is: because you incremented it. You applied the increment operator to it. When you apply the increment operator to a variable (doesn't matter, prefix or postfix), the variable gets incremented. Period.

Now, in both of your examples you apply increment operator to int1. And now you are asking why it got incremented? Sorry, a question like that just doesn't make any sense. If you don't know that increment operator increments variables, you need to read basic some book first before asking questions.

As for int2... You never apply increment operator to int2, so it will never be incremented. Never. There's no place in your example where int2 is incremented.

In both cases int2 is simply assigned a value. That value is the result of increment operator applied to int1. And all you need to know in this case is that postfix increment returns the old value of its operand (so int1++ evaluates to 14), while the prefix increment returns the new value of its operand (so, ++int1 evaluates to 15). This is what you get into int2 in those two cases.

Upvotes: 0

iwalkbarefoot
iwalkbarefoot

Reputation: 955

By using ++ either before or after a variable you are incrementing that variable regardless. What changes about putting it before or after is what is returned from the operation.

Value of int1

int1 = 1;
int2 = int++; //returns int1 value [1] and increments int1 by 1 [2] int3 = ++int; //increments int1 [2] by 1 [3] and then returns int1 [3]

Upvotes: 0

wilhelmtell
wilhelmtell

Reputation: 58667

The preincrement operator increments its operand and returns the incremented value:

int i = 0;
++i;  // now i is 1, and the returned 1 is discarded
int j = ++i;  // and now i is 2, and the returned 2 is stored in j

The postincrement increments its operand and returns the value before the increment:

int i = 0;
i++;  // now i is 1, and the returned 0 is discarded
int j = i++;  // now i is 2, and the returned 1 is stored in j

Upvotes: 1

sepp2k
sepp2k

Reputation: 370102

Both ++int1 and int1++ increment int1 because that's what the increment operators do: they increment their operand.

The reason that int2 is 15 in the first example and 14 in the second example because the pre-increment and post-increment operators differ in their return value: The post-increment operator increments the operand, but returns the value that the operand had before being incremented. The pre-increment operator returns the new value of the operand.

Upvotes: 2

Simon
Simon

Reputation: 32873

int1 = 14;
int2 = ++int1;

is equivalent to

int1 = 14;
int1 = int1 + 1;
int2 = int1;

and

int1 = 14;
int2 = int1++;

is equivalent to

int1 = 14;
int2 = int1;
int1 = int1 + 1;

Upvotes: 3

user460847
user460847

Reputation: 1618

With the pre-incrementing example, int1 is incremented and then assigned to int2. With the post-incrementing example, int1 is assigned to int2, and THEN int2 is incremented.

Upvotes: 0

Related Questions