Copyright © tutorialspoint.com

Euphoria - Short Circuit Evaluation

previous next


Advertisements

When a condition is tested by if, elsif, until, or while using and or or operators, a short-circuit evaluation will be used. For example:

if a < 0 and b > 0 then
   -- block of code
end if

If a < 0 is false, then Euphoria will not bother to test if b is greater than 0. It will know that the overall result is false regardless. Similarly:

if a < 0 or b > 0 then
   -- block of code
end if

if a < 0 is true, then Euphoria will immediately decide that the result true, without testing the value of b, since the result of this test would be irrelevant.

In General:

Whenever we have a condition of the form:

A and B

Where A and B can be any two expressions, Euphoria will take a short-cut when A is false and immediately make the overall result false, without even looking at expression B.

Similarly, Whenever we have a condition of the form:

A or  B

When A is true, Euphoria will skip the evaluation of expression B, and declare the result to be true.

Short-circuit evaluation of and and or takes place for if, elsif, until and while conditions only. It is not used in other contexts. For example:

x = 1 or {1,2,3,4,5} -- x should be set to {1,1,1,1,1}

If short-circuiting were used here, we would set x to 1, and not even look at {1,2,3,4,5}, which would be wrong.

Thus, short-circuiting can be used in if/elsif/until/while conditions because we only care if the result is true or false, and conditions are required to produce an atom as a result.


previous next

Copyright © tutorialspoint.com