0
post incrementing and decrementing
If I have if clause: if (x++>100) && (y++<10) statement else Statement ..... ..... next line Supposed I initialize x=99 and y=9 then if clause result true and next statement nested to it (single line) Will be performed and else clause is neglected. But if I initialize x=100 and y=9 or x=99 and y=10, the if clause Will return false and the else clause is executed. At this point, please let me know if the if-clause is false, then what is the value of x and y respectedly after the flow goes out from the if clause. Will the compiler increment 1 to x and y respectedly? Thank you
5 Respostas
+ 4
Oliver Pasaribu understanding this code requires knowing two critical concepts.
1. Post-increment
2. Short-circuit logic evaluation
Post-increment:
Whether x=99 or x=100, the if condition will evaluate x++ as 99, or 100, respectively. Afterward it will increment x to 100, or 101, respectively.
Note that the first conditional test will be false in either case.
(99>100) 👉 false
(100>100) 👉 false
Short-circuit logic evaluation:
The first test is false, and the next logical operator is a logical AND (&&). Since (false AND anything) is false, then after evaluating only the first clause there is enough information to determine that the final outcome will be false. Therefore execution skips the second test, and it moves on to the else clause.
Consequently, y++ will not get executed. Whether y=9, or y=10, it will remain 9, or 10, respectively.
+ 3
It would be easier to read with spaces, so the post increment operator appears attached to its variable but not to anything else.
if (x++ > 100) && (y++ < 10)
Remember that post increment and post decrement don't do anything until after the variable is evaluated in the expression, so if you say x = 99, then x++ has the value 99.
+ 3
if it's C++, you would need to enclose those in an outer ().
if((x++ > 100) && (y++ <10))
and yes, post increment will use the un-incremented values in the comparison.
But I feel that this is not a good way to write it because it makes the code harder to understand.
Just use
if((x > 100) && (y < 10)){
x++;
y++;
...
}else{
...
}
so you make it clearer that x and y are incremented, which can be easy to miss in your original code.
+ 2
Bob_Li : very good. You make it clear to me.
if ((x++>100) && (y<10))
{statements multiple}
Is identic to:
if (x>100 && y<10){
multiple statements;
x +=1;
y += 1;}
else{
multiple statements}
become easier. This if current x is >= 100 not fullfilled or current y is 10, then if statements inside if are not done. There Will be no increment by 1 to both x and y respectedly. Thank you friends. This is awesome.
+ 1
Brian
Great observation on the short-circuiting behavior. It is one we often forget.
Yes, putting the increment operation within the if statement is really a bad idea as it also introduces a hidden condition on whether the first comparison evaluates to true or false. Then depening on whether the boolean operator is && or ||, the second comparison-increment combination may or may not be executed. So the code becomes harder to read and reason about and is a potential source of hidden bugs.