When a condition is tested by if, elsif, until, or while using and or or operators, a short-circuit evaluation is used. For example −
In General, whenever you have a condition of the following form −
Similarly, whenever you have a condition of the following form −
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 −
Thus, short-circuiting can be used in if, elsif, until, or while conditions, because you need to only care if the result is true or false, and conditions are required to produce an atom as a result.
if a < 0 and b > 0 then -- block of code end ifIf a < 0 is false, then Euphoria does not bother to test if b is greater than 0. It knows that the overall result is false regardless. Similarly −
if a < 0 or b > 0 then -- block of code end ifif a < 0 is true, then Euphoria immediately decides that the result true, without testing the value of b, since the result of this test is irrelevant.
In General, whenever you have a condition of the following form −
A and BWhere A and B can be any two expressions, Euphoria takes a short-cut when A is false and immediately makes the overall result false, without even looking at expression B.
Similarly, whenever you have a condition of the following form −
A or BWhere A is true, Euphoria skips the evaluation of expression B, and declares 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, you 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, or while conditions, because you need to only care if the result is true or false, and conditions are required to produce an atom as a result.
No comments:
Post a Comment