Page 1 of 2
Does PB short-circuit expressions?
Posted: Wed Jun 30, 2004 7:52 pm
by matthew180
Greetings,
The subject pretty much sums it up... Does PB perform short-circuiting or expressions?
Thanks,
Matthew
Posted: Wed Jun 30, 2004 8:48 pm
by Karbon
Matthew. I already told you.
YES.
Don't make me come up there.
Posted: Wed Jun 30, 2004 9:00 pm
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@
Posted: Wed Jun 30, 2004 9:04 pm
by Kale
Does PB perform short-circuiting or expressions?
Whats the difference? *starts digging out computer science books*

Posted: Wed Jun 30, 2004 9:44 pm
by matthew180
Oops, that's supposed to be
on not
or...
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@
Posted: Thu Jul 01, 2004 12:10 am
by Kale
ah, i see, but still explain short circuiting please.

Posted: Thu Jul 01, 2004 1:29 am
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.

Posted: Thu Jul 01, 2004 8:42 am
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.
Posted: Thu Jul 01, 2004 9:40 am
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
Posted: Thu Jul 01, 2004 10:39 am
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.

Posted: Thu Jul 01, 2004 11:22 am
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.

Posted: Thu Jul 01, 2004 11:30 am
by Dare2
@tinman
Did I get the whole thing wrong? (again)

Posted: Thu Jul 01, 2004 12:13 pm
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.
Posted: Thu Jul 01, 2004 12:31 pm
by Dare2
Okay.

Posted: Thu Jul 01, 2004 12:51 pm
by Karbon
MAtthew is great at breaking code.. He's been breaking mine for years
