*Short-circuit evaluation:*

The logical operators **and**/**or** follow the short-circuit evaluation.

If the first argument of the **AND** operator is **zero**, then there is no need to evaluate the second argument and the result will be zero.

If the first arugment of the **OR** operator is **one**, then there is no need to evaluate the second argument and the result will be one.

*Example :*

x = 0#in boolean, it equals falsey = 10#in boolean, it equals true because it is greater than 0see (x and 1)#output : 0 (false) because x is false and number '1' is true, they both aren't true.x = 2#truesee (x and 1)#output : 1 (true) because they both are true values (greater than 0).x = 0#false valuesee (x or 1)#output : 1 (true) because on of the two is true and it is (number 1).

*Long Example :*

number_1 = 0#falsenumber_2 = 1#truenumber_3 = 5#truenumber_4 = 12#trueif (number_4 and number_3) see 'true' #output : true, because they both are true ok if (number_1 and number_4) see 'true' else see 'false'#output : false, because they both aren't true. number_1 is 0 (false)ok if (number_1 = 3) see 'true' else see 'false'#output : false, number_1 = 0 not 3ok if ( (number_1 = 0) and number_4) see 'true'#output : true, because (number_1 = 0) is true, and number_4 is trueelse see 'false' ok if ( (number_2 = 0) and number_1) see 'true' else see 'false'#output : false, because number_2 not = 0, it = 1 so (number_2 = 0) returnsokfalseand number_1 istrue, so all return false.

I hope you understand how it works now.

amrESSAM.

Advertisements