Menu
Topics Index
...
`

Short circuit AND - Java Example Program


Short circuit AND
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 );
    
    }
}
OUTPUT

e = 50

DESCRIPTION

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

THINGS TO TRY
  • Change the value of c to 1 in LINE A and see that the output will be e = 51
  • Change the value of c to 0 in LINE A and use logical AND (&) instead of short-circuit AND (&&) and see that the output will still be e = 51

© meritcampus 2019

All Rights Reserved.

Open In App