ANDs & ORs in If statements

Everything else that doesn't fall into one of the other PB categories.
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

ANDs & ORs in If statements

Post by endo »

Hi,
In if statements it exits on first "false" when condititions ANDed, but it does not exit on first "true" when condiditons ORed. Is it a design feature? As I know it is different some other languages. Please look at the examples below.

Code: Select all

Procedure test()
	MessageRequester("test","test")
	ProcedureReturn #True
EndProcedure

a = 0
If a = 1 And test() ; test() won't be called, and it is good..
	MessageRequester("ok","ok")
EndIf

Code: Select all

Procedure test()
	MessageRequester("test","test")
	ProcedureReturn #True
EndProcedure

a = 1
If a = 1 Or test() ; test() will be called
	MessageRequester("ok","ok")
EndIf
-= endo (registered user of purebasic since 98) =-
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ANDs & ORs in If statements

Post by Little John »

endo wrote:

Code: Select all

Procedure test()
	MessageRequester("test","test")
	ProcedureReturn #True
EndProcedure

a = 1
If a = 1 Or test() ; test() will be called
	MessageRequester("ok","ok")
EndIf
test is not called on my system (PB 4.30 x86 on Windows), so I can't reproduce your finding.
With which PureBasic version did you encounter this?

Regards, Little John
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Re: ANDs & ORs in If statements

Post by endo »

Little John wrote: test is not called on my system (PB 4.30 x86 on Windows), so I can't reproduce your finding.
With which PureBasic version did you encounter this?

Regards, Little John
its strange!? I'm using PB 4.30 x86 on Windows XP Pro.
-= endo (registered user of purebasic since 98) =-
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Works fine here as well in that test() is not called. (PB 4.3, Vista 32).
I may look like a mule, but I'm not a complete ass.
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

endo:
I think you are mistaking the "Ok" Messagerequester() for the "test" one.
In the second example, there has to be 1 requester, since the If evaluates to true. There is only something wrong if you get 2 requesters.
quidquid Latine dictum sit altum videtur
Marco2007
Enthusiast
Enthusiast
Posts: 648
Joined: Tue Jun 12, 2007 10:30 am
Location: not there...

Post by Marco2007 »

I would expect two requesters.
...and I get two, when doing this:

Code: Select all

Procedure test() 
   MessageRequester("test","test") 
   ProcedureReturn #True 
EndProcedure 

a = 1 
b=test()

If a = 1 Or test() ; test() will be called 
   MessageRequester("ok","ok") 
EndIf
or

Code: Select all

Procedure test() 
   MessageRequester("test","test") 
   ProcedureReturn #True 
EndProcedure 

a = 1 
b=test()

If a = 1 Or b ; test() will be called 
   MessageRequester("ok","ok") 
EndIf


and try this:

Code: Select all

Procedure test() 
   MessageRequester("test","test") 
   ProcedureReturn #True 
EndProcedure 

a = 1 
b=test()

If a = 1 Or test() ; test() will be called 
   MessageRequester("ok","ok") 
EndIf

If a = 1 Or test() ; test() will be called 
   MessageRequester("ok","ok") 
EndIf
PureBasic for Windows
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Marco2007 wrote:I would expect two requesters.
...and I get two, when doing this:
Of course you get two. You moved the function call out of the If statement.

In the original example, the expected number of requester is:
0 for the And example, because the statement is false even without the evaluation of test()
1 for the Or example, because the statement is true even without the test() (so you get only the requester inside the If)

This is called "Short-circuit evaluation" and a lot of languages do it this way, because it is faster and it allows to write code like this:

Code: Select all

If *Pointer <> 0 And PeekL(*Pointer) <> 0
  ; something
EndIf
If the second part got evaluated always, this code would crash whenever *Pointer is 0. But since the second part only gets tested if the first part was true, this code works without problems.
quidquid Latine dictum sit altum videtur
Marco2007
Enthusiast
Enthusiast
Posts: 648
Joined: Tue Jun 12, 2007 10:30 am
Location: not there...

Post by Marco2007 »

freak wrote:This is called "Short-circuit evaluation" and a lot of languages do it this way, because it is faster and it allows to write code like this: ....
.......
But since the second part only gets tested if the first part was true
I didn`t know that, although it makes sense!
Thank you :D
PureBasic for Windows
User avatar
idle
Always Here
Always Here
Posts: 5920
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

+ 1 on the didn't know.
Marco2007
Enthusiast
Enthusiast
Posts: 648
Joined: Tue Jun 12, 2007 10:30 am
Location: not there...

Post by Marco2007 »

Hm?
PureBasic for Windows
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Post by endo »

I could swear that test() function was called yesterday, but I guess Freak is right, it is not called if a=1.
Sorry for wasting your time :(

Code: Select all

Procedure test() 
   MessageRequester("test","test") 
   ProcedureReturn #True 
EndProcedure 

a = 1 
If a = 1 Or test() ; test() will *NOT* be called
   MessageRequester("ok","ok") 
EndIf
-= endo (registered user of purebasic since 98) =-
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Aye the short circuit evaluation is very useful and if this was ever changed all of my programs would instantly crash! :)
I may look like a mule, but I'm not a complete ass.
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Post by endo »

srod wrote:Aye the short circuit evaluation is very useful and if this was ever changed all of my programs would instantly crash! :)
yep, most useful cases are:

Code: Select all

If TryOpenScreen(320,200) Or TryOpenScreen(640,480) Or TryOpenScreen(800,600)
 ;ok
Else
 ;error
EndIf
And;

Code: Select all

If OpenConsole() And Init() And OpenWindow()
;ok
End If
So Init() can give console output, and OpenWindow() is not called if init failed.
-= endo (registered user of purebasic since 98) =-
Marco2007
Enthusiast
Enthusiast
Posts: 648
Joined: Tue Jun 12, 2007 10:30 am
Location: not there...

Post by Marco2007 »

idle wrote:+ 1 on the didn't know.
What does that mean?
PureBasic for Windows
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

Marco2007 wrote:
idle wrote:+ 1 on the didn't know.
What does that mean?
that does just mean that idle didn't know that, too, just like you. :P
oh... and have a nice day.
Post Reply