QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Dav on August 27, 2020, 10:02:29 pm

Title: Is this something the IDE should catch?
Post 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:

Code: QB64: [Select]
  1. c = 1
  2. a = b = c = d
  3. PRINT a, b, c, d

The above code outputs:  -1,   0,   1,   0

- Dav
Title: Re: Is this something the IDE should catch?
Post by: SpriggsySpriggs on August 27, 2020, 10:08:41 pm
That does seem strange. Interesting.
Title: Re: Is this something the IDE should catch?
Post by: SMcNeill on August 27, 2020, 10:18:39 pm
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
Title: Re: Is this something the IDE should catch?
Post by: SMcNeill on August 27, 2020, 10:26:33 pm
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.  ;)
Title: Re: Is this something the IDE should catch?
Post by: Dav on August 27, 2020, 10:50:23 pm
Ahhh...I see. I should have considered that was it.  The IDE is smarter than I am. Lol.

Thanks for the detailed explanation.

- Dav