QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: Dav on August 27, 2020, 10:02:29 pm
-
I messed up typing, and noticed that the IDE allows this line as OK: a = b = c = d, etc. Is that a bug? Code example:
The above code outputs: -1, 0, 1, 0
- Dav
-
That does seem strange. Interesting.
-
It's not a bug. It's a binary truth comparison.
c = 1
a = b = c = d
Let's just break down what we're dealing with here... For starters, there's only one assignment and two comparisons going on here, such as:
a = ((b = c) = d)
Now, if we had an IF statement which looked like IF b = c THEN, would it be TRUE (-1) or FALSE (0)?
b = 0. c = -1. So b = c is FALSE, so it has a value of 0. This simplifies our formula down to:
a = (0 = d)
Now, in this case, d is 0. Since 0 = 0, that's a TRUE statement, which gives a the value of -1.
And that prints your results as expected:
-1, 0, 1, 0
-
If you want to see it in action, just use PRINT:
C = 1
PRINT B = C 'this prints 0
PRINT 0 = D 'this prints -1
The assignment A = gets its value from the above 2 steps, as illustrated above. ;)
-
Ahhh...I see. I should have considered that was it. The IDE is smarter than I am. Lol.
Thanks for the detailed explanation.
- Dav