This question already has an answer here:
Aren't the individual expressions in a composite logical AND/OR expression supposed to be evaluated first before the logical operators are applied to their result?Why is ++k
untouched in the condition m = ++i && ++j || ++k
for the following program :
#include<stdio.h>
int main()
{
int i=-3, j=2, k=0, m;
m = ++i && ++j || ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
Output : -2,3,0,1
But I expect the output -2,3,1,1
You should avoid coding such unreadable code. It is actually parsed as
m = (++i && ++j) || ++k;
So once j >= 0
the ++j
condition is always true, so ++k
is not evaluated since &&
is a short-cutting and then but ||
is a short-cutting or else (so they may not evaluate their right operand).
So &&
is evaluated as follow: the left operand is evaluated, if it is false it is returned, and then only when it is true (i.e. not equal to 0
) the right operand is evaluated and returned as the evaluated result of the &&
. Likewise ||
is evaluated as follow: the left operand is evaluated. If it is true (non-zero) it becomes the result of the ||
; or else the right operand is evaluated and is the result of the ||
expression.
In particular, when coding if (x > 0 && 20/x < 5)
the division is never attempted for x==0
.
Read also the wikipedia operators in C & C++ & short circuit evaluation & lazy evaluation pages; and please take several hours to read a good C programming book.
Logical operators have short circuit evaluation, i.e. as soon as a value is determined for the expression, the rest of the expression is not evaluated.
e.g. m = ++i && ++j || ++k;
in this ++i -> true, ++j -> true (non zero value)
hence m = true && true || ++k;
now true && true is true so
m = true || ++k
As in OR operator if 1 side is true, the other is not evaluated so result is true.
Hence k is not incremented.
That is a shortcut for logical operators, in your case operator ||
. When the first operand is true
, it's impossible for the second operand to have any impact on the result. It will always be true, not matter what the second operand may yield. So the second operand is not evaluated.
The same goes for the logical &&
operator, if the first operand is found to be false
. The second operand will not matter, the result will always be false
and the second operand will therefor not be evaluated.
&&
and ||
are logical operators, and you are using them out of context which is odd (ok for C/C++ but you'd have type errors in Java or C#).
You've have just discovered short circuiting operators - you don't need to evaluate the whole expression if you "know" it's true. i.e. i and j are non zero so you don't need to do anything with k since you know the expression is true.
m = ++i && ++j || ++k;
Above line of code executes ++i and ++j first and doesn't execute ++k since it's written after || (OR)
Logical Circuit Operators don't execute when previously statement is true already in case of || and false in case of &&
Hence k is untouched
来源:https://stackoverflow.com/questions/16271779/why-isnt-k-incremented-in-the-statement-m-i-j-k-when-i