Author Topic: Is this something the IDE should catch?  (Read 3538 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Is this something the IDE should catch?
« 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

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Is this something the IDE should catch?
« Reply #1 on: August 27, 2020, 10:08:41 pm »
That does seem strange. Interesting.
Shuwatch!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Is this something the IDE should catch?
« Reply #2 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
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Is this something the IDE should catch?
« Reply #3 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.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Is this something the IDE should catch?
« Reply #4 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