Posts: 2,710
Threads: 328
Joined: Apr 2022
Reputation:
219
05-07-2024, 03:50 AM
(This post was last modified: 06-18-2024, 09:44 PM by RhoSigma.)
The original post here got lost due to a forum glitch, thankfully the first reader completely quoted it in his reply, so just read on in post #2.
Posts: 655
Threads: 96
Joined: Apr 2022
Reputation:
22
(05-07-2024, 03:50 AM)SMcNeill Wrote: To continue with our list of new commands, let me go over _ORELSE a bit today.
For starters, like before, let's have a little code to start with, so we can all be on the same page discussing things:
Code: (Select All)
a = 1
b = 2
If a Or b Then Print "True"
If a _Orelse b Then Print "true"
Print
Print a Or b
Print a _Orelse b
Now, there's a couple of different things which I'd like to note here about _ORELSE, in no particular order:
1) Like I mentioned yesterday with _ANDALSO, _ORELSE is a **LOGICAL** comparison. AND, and OR, are both **BINARY** comparison tools, so the behavior is going to be slightly different, as I'll explain in more detail below.
2) For most folks, the end result of using OR and _ORELSE, isn't really going to change much at all. I imagine in 99.9987% of most cases, one could swap out the two commands interchangably and never really notice any difference in how things perform at all.
WHY do I say such a thing for point #2??
Basically because it's true.
OR is a binary comparion which takes two values, compares them bit-by-bit, and ORs those bits to give us a third value. Since 0 is the **ONLY** value which BASIC counts as being FALSE, as long as one of those values is TRUE, the end result is going to evalutate to TRUE.
1 OR 2 <-- This is TRUE.
1 OR 0 <-- Even though the 2nd thing is FALSE, the first is TRUE, so the end result is TRUE.
0 OR 2 <-- Same way here. That first 0 is FALSE, but the second value is TRUE, so the end result is TRUE.
And, if we use _ORELSE, how does these values all play out??
1 _ORELSE 2 <-- This is TRUE.
1 _ORELSE 0 <-- Same as above. Even though the 2nd value is FALSE, the first isnt, so the end result is TRUE.
0 _ORELSE 2 <-- Same once again. The first value might be FALSE, but the second value isn't, so the end result is TRUE.
So what's the real difference in these two commands???
Code: (Select All) IF x OR y THEN
IF x _ORELSE y THEN
If both of those statements end up giving the same end results, what's the difference??
I'm glad I asked!!
OR, as I mentioned before, is a **BINARY** comparion method. Let's take a look at 1 OR 2:
00000001 <-- The value of 1, in binary
00000010 <-- The value of 2, written in binary.
----------- <-- Let's do an OR operation on those two values here
00000011 <-- This gives us an end result of 3.
1 OR 2 = 3.
OR works by comparing binary bits, and it basically performs a math-style function on the values, returning a value based on comparing those bits.
And how the heck does _ORELSE work??
1 _ORELSE 2 <-- we start with this statement.
1 <> 0 = -1 <-- it gets evaluated first to see if the 1 is <> 0. If it isn't, then the resulting value is -1 (TRUE).
2 <> 0 = -1 <-- the second value will get evaluated to make certain it's not 0. If it isn't, then the resulting value is -1 (TRUE).
Is either value -1? We check to see what those values are, and if either of them is -1, then the end result is -1 (TRUE).
1 OR 2 = 3 <-- binary usage of OR
1 _ORELSE 2 = -1 <-- LOGICAL usage of _ORELSE. This just tells us that one of these values is TRUE.
So, just as AND is for BINARY comparison/evaluation, OR is also for BINARY comparison/evaluation.
And, just like _ANDALSO is a LOGICAL comparison/evaluation, _ORELSE is a LOGICAL comparison/evaluation.
In most cases, you'd be able to swap between OR and _ORELSE in a basic comparison statement:
Code: (Select All) IF x OR y THEN
IF x _ORELSE y THEN
Both of the above are going to end up with the same style result in the end.
So what's the major differences in the two commands?
1) OR is a *BINARY* tool. I can't stress this enough. _ORELSE is a *LOGICAL* tool.
2) _ORELSE processes and evaluates slightly differently than OR does -- it has a built in short-circuit to it.
So what the heck is a short-circuit?? Let's take a moment and go over that!
1 OR 2 <-- this does math between the two values. BOTH values have to be taken in consideration, in order to get the end result. There's no way to short-circuit this, as we need to evaluate both sides of that OR and then compare their bits.
SQR(INT(RND * 123) + 5 ^ 3) OR (x + y - z * a / b \ c ^ d MOD e) <-- Now, if we're dealing with some very complex junk like what's to the left here, that's going to be quite a bit of processing going on! We've got to completely do the math on the left of the OR, and we've to do all the math on the right of that OR, and then we have to compare bits to get a final result!!
That's a lot of processing!!
And how's this apply to short-circuting??
SQR(INT(RND * 123) + 5 ^ 3) _ORELSE (x + y - z * a / b \ c ^ d MOD e) <-- Now here, we'd start at the left, evalute our value for SQR(INT(RND * 123) + 5 ^ 3)...
If this value is TRUE, then....
WHO GIVES A SQUAT ABOUT WHAT THE OTHER VALUE IS???
Logically, if one side of that _ORELSE is TRUE, then the result has to be TRUE.
**THAT** is short-circuiting, in a nutshell. _ORELSE doesn't care about the bit-result. It's not doing binary math. It's a LOGICAL operator, so as long as either side of that _ORELSE is true, then the whole statement is TRUE.
When dealing with some complex calculations, this _ORELSE can reduce a ton of processing cycles, just by skipping them completely.
This short-circuiting may be the greatest advantage that _ORELSE and _ANDALSO offers for us!!
Just remember: The return values for _ORELSE and _ANDALSO are **ALWAYS** going to be LOGICAL values. You'll either get -1 (TRUE) or you'll get 0 (FALSE) for the result. If you need binary comparisons, then **DO NOT** make the mistake of using these commands -- use AND and OR instead.
IF x AND 1 <-- This checks x to see if it has a binary value of 1. _ANDALSO would *really* suck to be used in this case.
a = a OR 32 <-- This sets the 6th bit of x to 1. _ORELSE would *really* suck here as well.
The commands are quite similar, and I imagine some folks will get confused with them over time, but keep in mind:
AND/OR are *BINARY* math operations.
_ANDALSO/_ORELSE are *LOGICAL* operations.
Don't fall into the habit of using _ANDALSO/_ORELSE everywhere in your code just because they have built in short-circuiting to them. Learn when you can use them (for logical comparisons), and when you can't (when you need binary comparisons). Otherwise you're going to end up writing some code which may just be almost impossible for anyone to ever debug and sort out.
a1 = SQR(INT(RND * 123) + 5 ^ 3) _ORELSE (x + y - z * a / b \ c ^ d MOD e).... <-- This is perfectly valid code.
a1 = SQR(INT(RND * 123) + 5 ^ 3) OR (x + y - z * a / b \ c ^ d MOD e).... <-- This too is perfectly valid code.
Both the above are going to run 100%. There's not going to be any IDE error, or compiler error... But whooo!!! They sure are going to end up giving different results and performing 100% differently in your code!!
And who among us, is going to be able to scan through 1000 lines of code written with formulas as complex as this, and pick out the subtle difference between them??
AND/OR are *BINARY* math operations.
_ANDALSO/_ORELSE are *LOGICAL* operations.
The above can not be stressed enough!! You were warned here first.
Thank God I'm a country boy! That's Waayy too complex for my little brain. I'll stay with AND and stay sane, ORELSE I'll go completely bonkers.
Posts: 388
Threads: 57
Joined: Apr 2022
Reputation:
13
I believe NOT is also a binary comparison like AND & OR. Might there be a use to also factor it into the world of logical operators _ORNOT , _ANDNOT ??
Posts: 2,710
Threads: 328
Joined: Apr 2022
Reputation:
219
(05-07-2024, 01:35 PM)Dimster Wrote: I believe NOT is also a binary comparison like AND & OR. Might there be a use to also factor it into the world of logical operators _ORNOT , _ANDNOT ?? Wait for it, for tomorrow.
Posts: 2,710
Threads: 328
Joined: Apr 2022
Reputation:
219
I have no idea what the heck happened to my original post, but I want to say, "Thank goodness for Phil!!" His reply/quote saves the day here!
All I did was change the title of the original post from a type of "Extended KotD #1" to "Extended KotD #2", and it somehow ended up losing/deleting all the contents of the post in that renaming!
THANK GOODNESS FOR PHIL!!!!
That's a lot of typing/writing/explaining which I'd hate to have to do all over again, just because the forums went stupid on us from a topic name change!
THANK GOODNESS FOR PHIL!!!!
Posts: 1,270
Threads: 118
Joined: Apr 2022
Reputation:
100
Reputation points for Phil all around!!
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
|