Recent

Author Topic: order of evaluation in an IF statement  (Read 688 times)

speter

  • Sr. Member
  • ****
  • Posts: 345
order of evaluation in an IF statement
« on: October 31, 2022, 04:06:59 am »
I have an IF statement like:
Code: Pascal  [Select][+][-]
  1. if func0 or func1 of func2 then ...
and I want the functions called "in order"; but I *think* (especially with optimisation) the order of calling the functions is not assured. (Each function returns a boolean.)

I can rewrite the code, to achieve what I want, using a "fake" Loop:
Code: Pascal  [Select][+][-]
  1. for a := 0 to 0 do
  2.   begin
  3.     if func0 then break;
  4.     if func1 then break;
  5.     if func2 then break;
  6.   end;
  7.  

but is "inelegant". :(

Is there a better way to ensure the functions are called in order!?

cheers
S.
Laz 2.2.0; FPC 3.2.2, Win 11.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2010
  • Fifty shades of code.
    • Delphi & FreePascal
Re: order of evaluation in an IF statement
« Reply #1 on: October 31, 2022, 04:43:40 am »
Code: Pascal  [Select][+][-]
  1. if (not func0) then
  2.   if (not func1) then
  3.     if (not func2) then
  4.       Exit;
  5. // here one of above matched in an ordered way... just my 50cents to that (but i believe that your single liner does do the same)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: order of evaluation in an IF statement
« Reply #2 on: October 31, 2022, 04:57:18 am »
Well, it will stop on the first true result if you string the checks together.

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. program shortcircuit;
  3.  
  4. function func0: boolean;
  5. begin
  6.   writeln({$I %LINE%});
  7.   exit(true)
  8. end;
  9.  
  10. function func1: boolean;
  11. begin
  12.   writeln({$I %LINE%});
  13.   exit(true)
  14. end;
  15.  
  16. function func2: boolean;
  17. begin
  18.   writeln({$I %LINE%});
  19.   exit(true)
  20. end;
  21.  
  22. procedure main;
  23.   begin
  24.     if func0 or func1 or func2 then writeln({$I %LINE%});
  25.   end;
  26.  
  27. begin
  28.   main;
  29. end.
  30.  

Code: Text  [Select][+][-]
  1. ./shortcircut
  2. 6
  3. 24
  4.  

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: order of evaluation in an IF statement
« Reply #3 on: October 31, 2022, 05:08:54 am »
Free Pascal is supposed to be the same as Delphi.

...
Operator precedence is 100% exactly the same as in Delphi. And mind that Boolean shortcut evaluation is ON by default.
This can have side effects in both Delphi and FPC but are the same side effects. So make sure that the code is compiled for the correct Boolean evaluation setting.


But it seems order might be guaranteed.

https://docwiki.embarcadero.com/RADStudio/Sydney/en/Expressions_(Delphi)#Operator_Precedence

Quote
An operator with higher precedence is evaluated before an operator with lower precedence, while operators of equal precedence associate to the left. Hence the expression:

Code: Pascal  [Select][+][-]
  1. X + Y * Z

multiplies Y times Z, then adds X to the result; * is performed first, because is has a higher precedence than +. But:

Code: Pascal  [Select][+][-]
  1. X - Y + Z

first subtracts Y from X, then adds Z to the result; - and + have the same precedence, so the operation on the left is performed first.

Since it is all the same operator,
Code: Pascal  [Select][+][-]
  1. or
according to that it should be left to right.

Also from the above page https://docwiki.embarcadero.com/RADStudio/Sydney/en/Expressions_(Delphi)#Complete_Versus_Short-Circuit_Boolean_Evaluation

Quote
Complete Versus Short-Circuit Boolean Evaluation

The compiler supports two modes of evaluation for the and and or operators: complete evaluation and short-circuit (partial) evaluation. Complete evaluation means that each conjunct or disjunct is evaluated, even when the result of the entire expression is already determined. Short-circuit evaluation means strict left-to-right evaluation that stops as soon as the result of the entire expression is determined. For example, if the expression
Code: Pascal  [Select][+][-]
  1. A and B
is evaluated under short-circuit mode when
Code: Pascal  [Select][+][-]
  1. A
is False, the compiler will not evaluate
Code: Pascal  [Select][+][-]
  1. B
; it knows that the entire expression is False as soon as it evaluates
Code: Pascal  [Select][+][-]
  1. A
.

But the key take away there is: Short-circuit evaluation means strict left-to-right evaluation
« Last Edit: October 31, 2022, 05:12:25 am by Bogen85 »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: order of evaluation in an IF statement
« Reply #4 on: October 31, 2022, 07:20:50 am »
Also from the above page https://docwiki.embarcadero.com/RADStudio/Sydney/en/Expressions_(Delphi)#Complete_Versus_Short-Circuit_Boolean_Evaluation

Quote
Complete Versus Short-Circuit Boolean Evaluation

The compiler supports two modes of evaluation for the and and or operators: complete evaluation and short-circuit (partial) evaluation. Complete evaluation means that each conjunct or disjunct is evaluated, even when the result of the entire expression is already determined. Short-circuit evaluation means strict left-to-right evaluation that stops as soon as the result of the entire expression is determined. For example, if the expression
Code: Pascal  [Select][+][-]
  1. A and B
is evaluated under short-circuit mode when
Code: Pascal  [Select][+][-]
  1. A
is False, the compiler will not evaluate
Code: Pascal  [Select][+][-]
  1. B
; it knows that the entire expression is False as soon as it evaluates
Code: Pascal  [Select][+][-]
  1. A
.

But the key take away there is: Short-circuit evaluation means strict left-to-right evaluation

It also means that - in the case of or-operators - as soon as a function returns True any function on the right of it in that expression won't be called.

speter

  • Sr. Member
  • ****
  • Posts: 345
Re: order of evaluation in an IF statement
« Reply #5 on: November 01, 2022, 12:52:50 am »
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Expressions_(Delphi)#Operator_Precedence

Quote
... Short-circuit evaluation means strict left-to-right evaluation that stops as soon as the result of the entire expression is determined...

But the key take away there is: Short-circuit evaluation means strict left-to-right evaluation

Many thanks to everyone, it seems I can leave my code as-is. :)

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

 

TinyPortal © 2005-2018