### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: if simplething OR complicatedthing then dosomething?  (Read 504 times)

#### dieselnutjob

• Full Member
• Posts: 143
##### if simplething OR complicatedthing then dosomething?
« on: February 11, 2020, 10:17:41 pm »
If I need a lump of code to run if either A or B is true then I would do

Code: Pascal  [Select]
1. if A or B then
2. begin
3.   dosomething;
4. end;
5.

But what if A is a really simple calculation (like are two bytes equal) whereas B is really complicated, like maybe even a function that goes and gets an answer from another computer over a network connection.

When the software runs presumably it has to actually calculate A and B before it can decide if (A OR B)=true ?

So I guess that actually if I don't want B to get calculated if A is true then I should do
Code: Pascal  [Select]
1. if A then
2. begin
3.   if B then
4.   begin
5.     dosomething;
6.   end;
7. end;
8.

and equally in my first version the function that calls the other computer over the network will ALWAYS do that even if A has already "sealed the deal"?

Is this correct or have I missed something?

thanks

#### HeavyUser

• Sr. Member
• Posts: 308
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #1 on: February 11, 2020, 10:34:13 pm »
If I need a lump of code to run if either A or B is true then I would do

Code: Pascal  [Select]
1. if A or B then
2. begin
3.   dosomething;
4. end;
5.

But what if A is a really simple calculation (like are two bytes equal) whereas B is really complicated, like maybe even a function that goes and gets an answer from another computer over a network connection.

When the software runs presumably it has to actually calculate A and B before it can decide if (A OR B)=true ?

So I guess that actually if I don't want B to get calculated if A is true then I should do
Code: Pascal  [Select]
1. if A then
2. begin
3.   if B then
4.   begin
5.     dosomething;
6.   end;
7. end;
8.

and equally in my first version the function that calls the other computer over the network will ALWAYS do that even if A has already "sealed the deal"?

Is this correct or have I missed something?

thanks
no its wrong. the code should go something like this
Code: Pascal  [Select]
1. If A then begin
2.   Do Something
3. end else if B then begin
4.   Do Something
5. end;
6.

#### jamie

• Hero Member
• Posts: 2495
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #2 on: February 11, 2020, 10:36:18 pm »
If I need a lump of code to run if either A or B is true then I would do

Code: Pascal  [Select]
1. if A or B then
2. begin
3.   dosomething;
4. end;
5.

But what if A is a really simple calculation (like are two bytes equal) whereas B is really complicated, like maybe even a function that

goes and gets an answer from another computer over a network connection.

When the software runs presumably it has to actually calculate A and B before it can decide if (A OR B)=true ?

So I guess that actually if I don't want B to get calculated if A is true then I should do
Code: Pascal  [Select]
1. if A then
2. begin
3.   if B then
4.   begin
5.     dosomething;
6.   end;
7. end;
8.

and equally in my first version the function that calls the other computer over the network will ALWAYS do that even if A has already "sealed the deal"?

Is this correct or have I missed something?

thanks

If (A = Sometthing ) or ((B = Something-A+Z)or(X-A=X)) Then...

Just put it all in a (…) to compound a booleaning statement.
Number 1 at blue screen app creations!

#### winni

• Hero Member
• Posts: 1146
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #3 on: February 11, 2020, 10:36:36 pm »
Hi!

There is -  as for  nearly everything - a compiler switch for the Boolean evaluation.

By default it is shortcut. But you can change that by:
Code: Pascal  [Select]
1. {\$ B+}

Now all boolean members are evaluated. Even if you code:

Code: Pascal  [Select]
1. If false and false  then DoImpossible;

Winni

#### dieselnutjob

• Full Member
• Posts: 143
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #4 on: February 11, 2020, 10:47:12 pm »
Hi!

There is -  as for  nearly everything - a compiler switch for the Boolean evaluation.

By default it is shortcut. But you can change that by:
Code: Pascal  [Select]
1. {\$ B+}

Now all boolean members are evaluated. Even if you code:

Code: Pascal  [Select]
1. If false and false  then DoImpossible;

Winni

and there was me thinking that it was a silly question....  I never would have guessed that.  Thanks

#### winni

• Hero Member
• Posts: 1146
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #5 on: February 11, 2020, 11:51:43 pm »
Hi!

Here is the list of the compiler switches from the Programmers  Manual:

https://www.freepascal.org/docs-html/prog/progch1.html#x5-40001

Winni

#### PascalDragon

• Hero Member
• Posts: 1112
• Compiler Developer
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #6 on: February 12, 2020, 09:19:32 am »
Please note that shorthand evaluation is enabled by default, so in your example:

Code: Pascal  [Select]
1. if A or B then
2. begin
3.   dosomething;
4. end;

B will already only be evaluated if A returned false.

• Hero Member
• Posts: 9809
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #7 on: February 12, 2020, 11:02:00 am »
in trunk you can do things like this:
Code: Pascal  [Select]
1. {\$mode delphi}{\$H+}
2. uses sysutils;
3. Type
4.   TFunction = function:string;
5. function f1:string;
6. begin
7.   result:='one is called';
8. end;
9.
10. function  f2:string;
11. begin
12.   result:= 'two is called';
13. end;
14.
15. var
16.   a:Tfunction = f1;
17.   b:Tfunction = f2;
18. begin
19.   writeln(ifthen<Tfunction>(true, a,b));
20.   writeln(ifthen<Tfunction>(false,a,b));
21. end.

That may be just what you need? The Boolean parameter can also be a Boolean expression of course.
I designed it to behave like that. It is also documented in the trunk docs.
« Last Edit: February 12, 2020, 01:13:20 pm by Thaddy »
I am more like donkey than shrek

#### PascalDragon

• Hero Member
• Posts: 1112
• Compiler Developer
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #8 on: February 13, 2020, 09:20:31 am »
Don't complicate things too much, Thaddy. dieselnutjob simply asked what the default short hand Boolean evaluation already provides.

#### uart

• New Member
• Posts: 40
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #9 on: February 19, 2020, 12:42:56 pm »
and there was me thinking that it was a silly question....  I never would have guessed that.  Thanks

Not a silly question, boolean short circuiting is a important feature of many compilers. https://en.wikipedia.org/wiki/Short-circuit_evaluation

Just remember that {\$B-) (enable boolean short circuit) is the default option, and you should always be mindful of boolean short circuits if you ever write any boolean functions that have side effects (meaning that they do something else important, other than just their boolean function). Otherwise you may end up in the situation where a function that really needs to be called (for your program to operate correctly) is not called because it has been short circuited.

• Hero Member
• Posts: 9809
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #10 on: February 19, 2020, 12:52:20 pm »
Indeed. A practical example is to disable boolean shortcuts in security related code to prevent timing attacks.
The rtl contains such code and for that reason. (I requested it)
I am more like donkey than shrek

#### uart

• New Member
• Posts: 40
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #11 on: February 19, 2020, 12:53:46 pm »
Now it's time for my silly question.

Under boolean short circuit settings {\$B-}, can we still guaranty that the first term in a boolean expression will be evaluated, even under all compiler optimization conditions?

Specifically, I want to know if there are any compiler options or conditions under which the order of a chain of boolean operations is altered such that the first listed  boolean condition is not evaluated.

I mean for example, in the following line of code, are there any conditions under which the compiler might evaluate the boolean expression in a different order and thereby short circuit my boolean function boolFn(x)?

if boolFn(x) and (a=b) then ...

• Hero Member
• Posts: 9809
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #12 on: February 19, 2020, 12:57:55 pm »
it is guaranteed left to right. here the compiler will never optimize order.
that also means you have to think if you can avoid the most costly
I am more like donkey than shrek

#### uart

• New Member
• Posts: 40
##### Re: if simplething OR complicatedthing then dosomething?
« Reply #13 on: February 19, 2020, 01:01:03 pm »
it is guaranteed left to right. here the compiler will never optimize order.

Thanks Thaddy, that's good to know.