[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.183 or_else

Synopsis

 
operator or_else (operand1, operand2: Boolean) = Result: Boolean;

Description

The `or_else' short-circuit logical operator performs the same operation as the logical operator `or'. But while the ISO standard does not specify anything about the evaluation of the operands of `or' -- they may be evaluated in any order, or not at all -- `or_else' has a well-defined behaviour: It evaluates the first operand. If the result is `True', `or_else' returns `True' without evaluating the second operand. If it is `False', the second operand is evaluated and returned.

GPC by default treats `or' and `or_else' exactly the same. If you want, for some reason, to have both operands of `or' evaluated completely, you must assign both to temporary variables and then use `or' -- or `or_else', it does not matter.

Conforming to

`or_else' is an ISO-10206 Extended Pascal extension.

Some people think that the ISO standard requires both operands of `or' to be evaluated. This is false. What the ISO standard does say is that you cannot rely on a certain order of evaluation of the operands of `or'; in particular things like the following program can crash according to ISO Pascal, although they cannot crash when compiled with GNU Pascal running in default mode.

 
program OrBug;
var
  a: Integer;
begin
  ReadLn (a);
  if (a = 0) or (100 div a > 42) then  { This is NOT safe! }
    WriteLn ('You''re lucky. But the test could have crashed ...')
end.

Example

 
program Or_ElseDemo;
var
  a: Integer;
begin
  ReadLn (a);
  if (a = 0) or_else (100 div a > 42) then  { This is safe. }
    WriteLn ('100 div a > 42')
end.

See also

section 9.182 or else, section 9.180 or, section 9.9 and_then.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Frank Heckenbach on May, 10 2002 using texi2html