N

The difference is that ++x first increases the value of x, and only then uses that value for whatever it is being used. Already x++ first uses the value of x in the expression, and only then increase. 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 worth zero, x > 0 is not true, so does not enter into if. After that x is used in comparison, it is incremented, and the code prints:1Now 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 comparison x > 0 (which is now true, since x 1). So the code prints:entered the if1So 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);
O for will run 7 times. Like z is not used within the loop (it is only used to control the amount of times that the for wheel), we can simply analyze the values of x and y each of the 7 iterations.Remembering that, as we have &&, that means that both conditions must be true to enter into if. If the first one is false, he'll even evaluate the second.In the first iteration, ++x > 2 increase x, which is worth 1. Then the comparison > 2 is false, and therefore the second condition is not even tested. So now we have to x worth 1 and y remains zero (and does not enter if).In the second iteration, x is incremented to 2, the condition > 2 is false and y is not evaluated, and therefore remains zero (and still does not enter the if).In the third iteration, x is increased to 3 and now the condition > 2 It's true. But how && requires both conditions to be true, he needs to test y. Then y++ > 4 first test the condition > 4 (which is false, for y is zero) and then increment y, which is worth 1. So you still don't get in if.In the fourth iteration, x is incremented to 4 and the condition > 2 It's true. Then y++ > 4 testa the condition > 4, which is still false (for y still worth 1) and y is incremented to 2. And he's not in yet if.In the fifth iteration, x is incremented to 5 and the condition > 2 It's true. Then y++ > 4 testa the condition > 4, which is still false (for y still worth 2) and y is incremented to 3. And he's not in yet if.On Friday iteration, x is incremented to 6 and the condition > 2 It's true. Then y++ > 4 testa the condition > 4, which is still false (for y still worth 3) and y is incremented to 4. And he's not in yet if.In the seventh iteration, x is incremented to 7 and the condition > 2 It's true. Then y++ > 4 testa the condition > 4, which is still false (for y still worth 4) and y is incremented to 5. And he's not in yet if.Like 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 if possesses ||, which means that if one of the conditions is true, it is enough. So if the first one is true, he won't even test the second one.In the first iteration, x is incremented to 1 and the comparison > 3 It's fake. But || only needs one of the conditions to be true, so he tests the next. y is incremented to 1 and the test > 2 It's fake. Then you don't get in if.In the second iteration, x is incremented to 2 and the comparison > 3 It's fake. y is incremented to 2 and the test > 2 It's fake. Then you don't get in if.In the third iteration, x is incremented to 3 and the comparison > 3 It's fake. y is incremented to 3 and the test > 2 It's true. So now you come in if, increasing x to 4.In the fourth iteration, x is incremented to 5 and the comparison > 3 It's true. Like || only needs one of the conditions to be true, it does not test the other (then y remains being 3). And as the first comparison was true, it enters into if, increasing x to 6.On Friday iteration, x is incremented to 7, the comparison > 3 is true, enters if and x is incremented to 8.In the end, we have to x it's 8 and y It's three.