The difference is that ++x
first increases the value of x
, and only then use this value for whatever it is being used. Now x++
first uses the value of x
in the expression, and only then increments. A simple example:
int x = 0;
if (x++ > 0) {
printf("entrou no if\n");
}
printf("%d\n", x);
This example first uses the value of x
in comparison. How x
valley zero, x > 0
is not true, so does not enter the if
. After the x
is used in comparison, it is incremented, and the code prints:
1
Now if we do:
int x = 0;
if (++x > 0) {
printf("entrou no if\n");
}
printf("%d\n", x);
In that case, first x
is incremented and its value becomes 1. After that it is used in the comparison x > 0
(which is now true, since x
is worth 1). That’s why the code prints:
entered the if
1
So let’s understand the codes.
int x = 0, y = 0, z = 0;
for(z = 0; z < 7; z++)
if((++x > 2) && (y++ > 4))
x++;
printf("x: %d y: %d\n", x, y);
The for
will run 7 times. Like the z
is not used within the loop (it is only used to control the amount of times the for
wheel), we can simply analyze the values of x
and y
each of the 7 iterations.
Remembering that, as we have &&
, this means that both conditions must be true to enter the if
. If the first one is false, he won’t even rate the second.
In the first iteration, ++x > 2
increment x
, which becomes worth 1. Then the comparison > 2
is false, and so the second condition is not even tested. So now we have to x
is worth 1 and y
remains zero (and does not enter the if
).
In the second iteration, x
is increased to 2, the condition > 2
is false and y
neither is evaluated, and therefore remains zero (and does not yet enter the if
).
In the third iteration, x
is increased to 3 and now the condition > 2
is true. But how &&
requires both conditions to be true, he needs to test y
. So y++ > 4
first test the condition > 4
(which is false because y
is zero) and then increments y
, worth 1. That’s why you don’t enter the if
.
In the fourth iteration, x
is increased to 4 and the condition > 2
is true. So y++ > 4
test the condition > 4
, which is still false (because y
is still worth 1) and y
is incremented to 2. And still does not enter the if
.
In the fifth iteration, x
is increased to 5 and the condition > 2
is true. So y++ > 4
test the condition > 4
, which is still false (because y
is still worth 2) and y
is incremented to 3. And still does not enter the if
.
In the sixth iteration, x
is increased to 6 and the condition > 2
is true. So y++ > 4
test the condition > 4
, which is still false (because y
is still worth 3) and y
is incremented to 4. And still does not enter the if
.
In the seventh iteration, x
is increased to 7 and the condition > 2
is true. So y++ > 4
test the condition > 4
, which is still false (because y
is still worth 4) and y
is incremented to 5. And still does not enter the if
.
Like the for
only runs 7 times, the loop ends, with x
equal to 7 and y
equal to 5.
int x = 0, y = 0, z = 0;
for(z = 0; z < 5; z++)
if((++x > 3) || (++y > 2))
x++;
printf("x: %d y: %d\n", x, y);
Similarly, the loop runs 5 times. But now the if
possesses ||
, which means that if one of the conditions is true, it’s enough. So if the first one is true, it doesn’t even test the second.
In the first iteration, x
is incremented to 1 and the comparison > 3
is false. But the ||
only needs one of the conditions to be true, so he tests the next. y
is incremented to 1 and the test > 2
is fake. So do not enter the if
.
In the second iteration, x
is increased to 2 and the comparison > 3
is false. y
is incremented to 2 and the test > 2
is fake. So do not enter the if
.
In the third iteration, x
is increased to 3 and the comparison > 3
is false. y
is incremented to 3 and the test > 2
is true. So now get into if
, increasing x
for 4.
In the fourth iteration, x
is increased to 5 and the comparison > 3
is true. As the ||
just needs one of the conditions to be true, it doesn’t even test the other (so y
remains 3). And as the first comparison was true, he enters the if
, increasing x
for 6.
In the sixth iteration, x
is incremented to 7, the comparison > 3
It’s real, get into the if
and x
is incremented to 8.
In the end, we have to x
is 8 and y
is 3.
I have difficulty understanding "x++ vs++x"
– Guilherme Ioshua Belmont