The PL/I standard specifies that all expressions in an IF-Statement are evaluated to determine truth or falsity. However the IBM Enterprise PL/I Language Reference says:
The test of the IF expression is short-circuited under the following circumstances:
• If the IF expression consists of a logical OR of 2 expressions and the first of these expressions is true,
the second expression will not be evaluated and the code will execute the THEN clause.
• If the IF expression consists of a logical AND of 2 expressions and the first of these expressions is false,
the second expression will not be evaluated and the code will execute the ELSE clause.
Iron Spring PL/I currently short-circuits. I have tested Windows PL/I and Multics PL/I and they don't. I am attaching a small program to test this. I'd appreciate it if someone could compile it with whatever PL/I compiler you have and let me know the results. If it short-circuits you should get two lines of output: "zero" and "one"; if it does not you should get four.
Normally this is not a problem, except for two cases:
1. Evaluation of the second part of the expression depends on the first being true or false, for example "if p¬null & p->"...
2. One or more of the expressions has side-effects, as in the following example.
---------- the program ----------
/* Test shortcut IF-Statements */
if : Procedure options ( main ) ;
zero: proc returns( bit(1) );
put skip list('zero');
return(0);
end zero;
one: proc returns( bit(1) );
put skip list('one');
return(1);
end one;
if 0 & zero() then /*nothing*/ ;
if 1 & zero() then /*nothing*/ ;
if 0 | one() then /*nothing*/ ;
if 1 | one() then /*nothing*/ ;
End if ;
Short-circuiting IF statements
- From
- Peter Flass <Pet…@Iron-Spring.com>
- Organization
- A noiseless patient Spider
- Message-ID
- <11b0…@dont-email.me>
- Date
- Newsgroups
- comp.lang.pl1