Ring Documentation Walkthrough #5.2 – Short-circuit evaluation

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 false
y = 10 #in boolean, it equals true because it is greater than 0

see (x and 1) #output : 0 (false) because x is false and number '1' is true, they both aren't true.

x = 2 #true
see (x and 1) #output : 1 (true) because they both are true values (greater than 0).

x = 0 #false value
see (x or 1) #output : 1 (true) because on of the two is true and it is (number 1).

Long Example :

number_1 = 0 #false
number_2 = 1 #true
number_3 = 5 #true
number_4 = 12 #true

if (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 3
ok 

if ( (number_1 = 0) and number_4) see 'true' #output : true, because (number_1 = 0) is true, and number_4 is true
else 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) returns false and number_1 is true, so all return false.
ok

I hope you understand how it works now.

amrESSAM.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s