Does PB short-circuit expressions?
-
- User
- Posts: 64
- Joined: Mon Jun 30, 2003 5:36 pm
- Location: Michigan
- Contact:
Does PB short-circuit expressions?
Greetings,
The subject pretty much sums it up... Does PB perform short-circuiting or expressions?
Thanks,
Matthew
The subject pretty much sums it up... Does PB perform short-circuiting or expressions?
Thanks,
Matthew
Matthew. I already told you.
YES.
Don't make me come up there.
YES.
Don't make me come up there.
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
-
- User
- Posts: 64
- Joined: Mon Jun 30, 2003 5:36 pm
- Location: Michigan
- Contact:
SILENCE!
It's not specifically stated in the manual and I didn't feel like writing a test. However, I did write a test program anyway and you are correct, it does do short circuit. But I also wanted the question here in the forum for other people to find when they search (I'm sure the question will come up again.)
M@
It's not specifically stated in the manual and I didn't feel like writing a test. However, I did write a test program anyway and you are correct, it does do short circuit. But I also wanted the question here in the forum for other people to find when they search (I'm sure the question will come up again.)
M@
-
- User
- Posts: 64
- Joined: Mon Jun 30, 2003 5:36 pm
- Location: Michigan
- Contact:
[EDIT]
This post deleted because the information herein was erroneous.
Grrr. I felt like saying: This post intentionally left blank. This is a true reflection of the IQ level of the poster.

This post deleted because the information herein was erroneous.
Grrr. I felt like saying: This post intentionally left blank. This is a true reflection of the IQ level of the poster.

Last edited by Dare2 on Thu Jul 01, 2004 12:30 pm, edited 1 time in total.
@}--`--,-- A rose by any other name ..
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
[quote="Dare2]A quick example cure:[/quote]
Short circuit evaluation is a good thing. It means you don't need to evaluate both sides of a logical expression to get the result. Therefore if you have a simple comparison logically combined with an evil complex one, you're best putting the simple one on the left because it may mean you can avoid carrying out the right hand side thus saving you time.
Conversely, if you have e.g. a procedure which must always be evaluated, you should put it on the left.
The topic you (IIRC) started seems to indicate a bug.
Short circuit evaluation is a good thing. It means you don't need to evaluate both sides of a logical expression to get the result. Therefore if you have a simple comparison logically combined with an evil complex one, you're best putting the simple one on the left because it may mean you can avoid carrying out the right hand side thus saving you time.
Conversely, if you have e.g. a procedure which must always be evaluated, you should put it on the left.
The topic you (IIRC) started seems to indicate a bug.
Last edited by tinman on Thu Jul 01, 2004 10:17 am, edited 1 time in total.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
(WinXPhSP3 PB5.20b14)
Short circuiting allows for some neat tricks:
viewtopic.php?t=9926&highlight=die
The thing to remember is that when you have exp1 OR exp2, exp2 will only get run if exp1 returns fales. This can be useful if you want to handle the expception.
Fred advices against the use as I describe it in the above post. Using expressions as commands can have unexpected results.
The way I do it now is:
The calls to Die only get made if the first function returns 0.
With And the opposite is true. The second expression only gets executed if the first is successful. This is useful for dependancies. For example:
OpenScreen is only called if InitSprite is successful. I think this is tidier than nesting Ifs. More complex expressions are possible, such as:
You just have to avoid making it incomprehensible 
Here's a working sample to cut and paste:
viewtopic.php?t=9926&highlight=die
The thing to remember is that when you have exp1 OR exp2, exp2 will only get run if exp1 returns fales. This can be useful if you want to handle the expception.
Fred advices against the use as I describe it in the above post. Using expressions as commands can have unexpected results.
The way I do it now is:
Code: Select all
If OpenWindow(...) or Die("Failed to Open Window)
If If CreateGadgetList(WindowID()) or Die("Failed to Create Gadget List")
[...]
EndIf
EndIf
With And the opposite is true. The second expression only gets executed if the first is successful. This is useful for dependancies. For example:
Code: Select all
If InitSprite() And OpenScreen(x1, y1, 16, "Title")
[...]
EndIf
Code: Select all
If ( OpenWindow(...) and CreateGadgetList(WindowID() ) or Die("Window Error")
[...]
EndIf

Here's a working sample to cut and paste:
Code: Select all
Procedure Die(Message.s)
MessageRequester("Fatal Error", Message)
End
EndProcedure
#Flags = #PB_Window_WindowCentered | #PB_Window_SystemMenu
If (OpenWindow(0, 0, 0, 100, 100,#Flags , "Test") And CreateGadgetList(WindowID())) Or Die("Window Error")
EditorGadget(0, 0, 0, 100, 100)
EndIf
Repeat
evt = WaitWindowEvent()
Until evt = #PB_Event_CloseWindow
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
Just that short circuit evaluation in itself isn't a bug or problem, but it does seem to be broken at the moment.Dare2 wrote:@tinman
Did I get the whole thing wrong? (again) :?
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
(WinXPhSP3 PB5.20b14)
MAtthew is great at breaking code.. He's been breaking mine for years 

-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net