CODE
class ShortCircuitAnd
{
public static void main(String arg[])
{
int c = 0, d = 100, e = 50; // LINE A
if( c == 1 && e++ < 100 )
{
d = 150;
}
System.out.println("e = " + e );
}
}
e = 50
Since c
is 0
, which is not equal to one, the left hand operand is false and hence right hand operand is not evaluated and hence e
is not incremented. But if c
is set to 1
, then left hand operand will be true and hence right hand operand will be evaluated, thus incrementing e
, hence the output will be e = 51
c
to 1
in LINE A
and see that the output will be e = 51c
to 0
in LINE A
and use logical AND (&
) instead of short-circuit AND (&&
) and see that the output will still be e = 51