Does PB short-circuit expressions?

Everything else that doesn't fall into one of the other PB categories.
matthew180
User
User
Posts: 64
Joined: Mon Jun 30, 2003 5:36 pm
Location: Michigan
Contact:

Does PB short-circuit expressions?

Post by matthew180 »

Greetings,

The subject pretty much sums it up... Does PB perform short-circuiting or expressions?

Thanks,
Matthew
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Matthew. I already told you.

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
matthew180
User
User
Posts: 64
Joined: Mon Jun 30, 2003 5:36 pm
Location: Michigan
Contact:

Post by matthew180 »

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@
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Does PB perform short-circuiting or expressions?
Whats the difference? *starts digging out computer science books* :oops:
--Kale

Image
matthew180
User
User
Posts: 64
Joined: Mon Jun 30, 2003 5:36 pm
Location: Michigan
Contact:

Post by matthew180 »

Oops, that's supposed to be on not or... :oops:

I probably should have asked something more along the lines of:

Does PB do short-circuit during the evaluation of expressions?

Sorry for the confusion!

M@
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

ah, i see, but still explain short circuiting please. :wink:
--Kale

Image
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

[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.

:)
Last edited by Dare2 on Thu Jul 01, 2004 12:30 pm, edited 1 time in total.
@}--`--,-- A rose by any other name ..
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

[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.
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)
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

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:

Code: Select all

If OpenWindow(...) or Die("Failed to Open Window)
    If If CreateGadgetList(WindowID()) or Die("Failed to Create Gadget List")
       [...]
    EndIf
EndIf
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:

Code: Select all

If InitSprite() And OpenScreen(x1, y1, 16, "Title")
   [...]
EndIf
OpenScreen is only called if InitSprite is successful. I think this is tidier than nesting Ifs. More complex expressions are possible, such as:

Code: Select all

If ( OpenWindow(...) and CreateGadgetList(WindowID() ) or Die("Window Error")
       [...]
EndIf
You just have to avoid making it incomprehensible :)

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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> 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.

True, and I like that aspect of PureBasic. I vote to keep it that way, because
I can always just use brackets if I ever need to force an equation. ;)
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

I've just realised i do understand 'short circuiting' and why its a good thing, i've just never heard it called that before. :)
--Kale

Image
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

@tinman

Did I get the whole thing wrong? (again) :?
@}--`--,-- A rose by any other name ..
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Dare2 wrote:@tinman

Did I get the whole thing wrong? (again) :?
Just that short circuit evaluation in itself isn't a bug or problem, but it does seem to be broken at the moment.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Okay. :?
@}--`--,-- A rose by any other name ..
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

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
Post Reply