First let's write it as a proper
C
program (as
C++
is not
C
):
#include <stdio.h>
int main()
{
int i=4, j=-1, k=0, w, x, y, z;
w = i || j || k;
x = i && j && k;
y = i || j && k;
z = i && j || k;
printf("w=%d x=%d y=%d z=%d\n", w, x, y, z);
getchar();
return 0;
}
Then, consider
w = i || j || k = (4) || (-1) || (0) = true AND true AND true = true = 1;
where the expressions with 'true', 'AND', etc. are 'imaginary', just there to illustrate how evaluation happens.
x = i && j && k = .. = true AND true AND false = false = 0;
y = i || j && k = .. = true OR (true AND false) = true OR false = true = 1;
z = i && j || k = .. = (true AND true) OR false = true OR false = true = 1;
in the latest two expressions you see the
C
operator precedence rules[
^] in action (the precedence of
&&
operator is higher than the precedence of the
||
operator).