[Ada Information Clearinghouse]
Ada '83 Rationale, Sec 3.10: Short-Circuit Control Forms

"Rationale for the Design of the
Ada® Programming Language"

[Ada '83 Rationale, HTML Version]

Copyright ©1986 owned by the United States Government. All rights reserved.
Direct inquiries to the Ada Information Clearinghouse at adainfo@sw-eng.falls-church.va.us.

CHAPTER 3: Classical Programming

3.10 Short-Circuit Control Forms

The operands of a boolean expression such as A and B can be evaluated in any order. Depending on the complexity of the term B, it may be more efficient (on some but not all machines) to evaluate B only when the term A has the value TRUE. This however is an optimization decision taken by the compiler and it would be incorrect to assume that this optimization is always done. In other situations we may want to express a conjunction of conditions where each condition should be evaluated (has meaning) only if the previous condition is satisfied.

Both of these things may be done with short-circuit control forms such as:

if NUMBER /= 0  and then  TOTAL/NUMBER > MEDIAN then
end if;

Clearly it would not be proper to express this condition as a boolean expression using the and operator, since an exception would be raised if NUMBER were zero and the second operand were evaluated. Similarly, short-circuit disjunctions can be expressed with or else clauses as in the following example:

    exit when NEXT =  null  or else  NEXT.AGE =  0;

In this case the condition following or else will only be evaluated if the previous condition is not satisfied.

In Algol 60 one can achieve the effect of short-circuit evaluation only by use of conditional expressions, since complete evaluation is performed otherwise. This often leads to constructs that are tedious to follow:

    if(if NUMBER =  0 then TRUE else TOTAL/NUMBER > MEDIAN) then ...

Several languages do not define how boolean conditions are to be evaluated. As a consequence programs based on short-circuit evaluation will not be portable. This clearly illustrates the need to separate boolean operators from short-circuit control forms.

Address any questions or comments to adainfo@sw-eng.falls-church.va.us.