Author Topic: True and False  (Read 2006 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
True and False
« on: February 09, 2022, 12:23:13 pm »
True and False in QB64 are not key words. At least I don't think they are. They seem to be key words in other languages. In QB64 I can make False = 1 or 10 or 1000 and True = Not False. It seems to me that I need to interpret True or False from the results of an equation. The wiki states True = -1 and False = 0. Quite often I'm not getting those values (particularly for False) even though I know what the correct result of the outcome should be.

True and False seem to be in the background somewhere....If a = 5 then etc ....has a true/false element being performed but we only work with the Then part of the result. I'm wondering, if the STATUS screen at the bottom of the IDE could display decision statements and then either TRUE or FALSE (rather than -1 or 0) to those decision statements in our coding. If not, is it possible to add that feature to Debug in the future?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: True and False
« Reply #1 on: February 09, 2022, 01:16:56 pm »
The wiki states True = -1 and False = 0. Quite often I'm not getting those values (particularly for False) even though I know what the correct result of the outcome should be.

This isn't quite true with regard to TRUE/FALSE in the BASIC language.

False = 0.   <---   This is our hard and set fact in BASIC.  Zero = FALSE.

**ANYTHING ELSE** = TRUE.

-1 is true.   3 is true.   456.23 is true.   _PI is true.   Anything NOT zero is true.



The reason why people use -1 to represent true, when any value non-zero would work truthfully, is for ease of logical operations.

0 = FALSE
NOT 0 = -1   (NOT FALSE = TRUE)
NOT -1 = 0   (NOT TRUE = FALSE)

Since FALSE is always going to be 0, then NOT FALSE would always be -1.

But as for what's actually evaluated as being a TRUE or FALSE statement, not talking "logical operator based", try out this little program below:

Code: QB64: [Select]
  1. For i = -10 To 10
  2.     Print i;
  3.     If i Then Print "TRUE" Else Print "FALSE"
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline CharlieJV

  • Newbie
  • Posts: 89
Re: True and False
« Reply #2 on: February 09, 2022, 01:35:15 pm »
And now that a proper explanation and example has been given, I am free to throw in and example from the far side:

Code: QB64: [Select]
  1. if len("Paint me green and call me Gumby.") then
  2.     print "TRUE!"

Offline Dimster

  • Forum Resident
  • Posts: 500
Re: True and False
« Reply #3 on: February 09, 2022, 02:18:08 pm »
Steve - Well I have never looked at it from that perspective.

False didn't mean zero, it meant the equation was producing the wrong value. So False had numbers just like your True example values. I'm still thinking (in your example) why i @ 0 isn't TRUE. The loop control values included a zero, so the range of -10 to 10 does include the "value" zero and the loop doesn't appear to me to be a decisional type of statement but rather a simple printing of the index "value"... not a test of True or False???

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: True and False
« Reply #4 on: February 09, 2022, 02:40:13 pm »
Code: QB64: [Select]
  1. If i Then Print "TRUE" Else Print "FALSE"
  2.  

Is same as
Code: QB64: [Select]
  1. If i <> 0 Then Print "TRUE" Else Print "FALSE"

IF looks at (value of expression) and THEN takes True path (value <> 0) Else takes False path (value = 0)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: True and False
« Reply #5 on: February 09, 2022, 02:45:07 pm »
Here's a trick when working with IF statements --  Consider them to all have an implied " <> 0 " that they evaluate against.

IF i THEN...

In all truth, the above statement evaluates as if:

IF i <> 0 THEN...     The <> 0 is the implied final comparison.

So when i = -10, the check for -10 <> 0 is true.
when i = -9, the check for -9 <> 0 is true.

When we get down to i = 0, the check for 0 <> 0 is false.



Even if you have a more explicit IF statement, that implied <> 0 is still there at the end.

a = 3
IF a <> 2 THEN  ...

The above gets resolved basically as IF (a <> 2) <> 0 THEN...  (the red is the implied truth check)
a <> 2 becomes 3 <> 2...  that's true, so we have a result of -1.  (The value BASIC returns for truth comparisons.)
-1 <> 0 is true...   So, at the end of the day, a <> 2 is true.



Change the value of a to 2, and we find:

a = 2
IF a <> 2 THEN....

Same as before, the above gets resolved basically as IF (a <> 2) <> 0 THEN...  (the red is the implied truth check)
a <> 2 becomes 2 <> 2...  that's FALSE, so we have a result of 0.  (Remember, in BASIC, FALSE is always ZERO.)
0 <> 0 is false...   So, at the end of the day, a <> 2 is false when a is 2.



All IF statements basically have an implied <> 0 to their evaluation.  When you keep that in mind, the FOR.. NEXT loop which I showed above makes perfect sense.

For i = -10 To 10
    Print i;
    If i <> 0 Then Print "TRUE" Else Print "FALSE"
Next



If it's not false (ZERO), then it's true.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
Re: True and False
« Reply #6 on: February 09, 2022, 03:43:47 pm »
Ok, we need to hook up at a bar and have a beer and talk this through.... so you guys are saying the IF is in fact a decision and not a simple ON arriving at the index zero, print the index zero?? The IF statement in your example Steve simply says "IF a", which I have always read "On reaching a " or " If the loop index has reached a .. print it's value"

It seems, even if I make a = 0, which should be a True statement   (ie a = 0  : If a then Print "True" else Print "False" comes up with False)   we don't take away the decisional aspect of "If a"

I appreciate the lesson here. Thanks guys.


Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: True and False
« Reply #7 on: February 09, 2022, 03:51:07 pm »
Ok, we need to hook up at a bar and have a beer and talk this through.... so you guys are saying the IF is in fact a decision and not a simple ON arriving at the index zero, print the index zero?? The IF statement in your example Steve simply says "IF a", which I have always read "On reaching a " or " If the loop index has reached a .. print it's value"

It seems, even if I make a = 0, which should be a True statement   (ie a = 0  : If a then Print "True" else Print "False" comes up with False)   we don't take away the decisional aspect of "If a"

I appreciate the lesson here. Thanks guys.

IF a = 0 THEN...

Remember the implied <> 0, which lets us evaluate as

IF (a = 0) <> 0 THEN...

In the case that a = 0, that evaluates as:

a = 0....    Since A is zero, this is true.  We return a -1 to the condition.

Then the implied <> 0 is checked against:

-1 <> 0...   This is true, so the if check gives you a TRUE result and passes.

IF is, in fact, a decision branch.  It evalutes your condition and determines if it's equal to zero (FALSE), or anything else (TRUE).
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
Re: True and False
« Reply #8 on: February 09, 2022, 05:50:50 pm »
i like to avoid "if" and describe it with action

if a> 5 then b = b +1

this can also be the case:
b = b + abs (a> 5)

an operation can often be expressed faster

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
Re: True and False
« Reply #9 on: February 09, 2022, 05:55:17 pm »
if a> 5 then b = b +1

this can also be the case:
b = b + abs (a> 5)

nice mod!

Offline Dimster

  • Forum Resident
  • Posts: 500
Re: True and False
« Reply #10 on: February 10, 2022, 09:49:04 am »
@MasterGy ... This is another great example of why True and False become somewhat confusing for me.

I'm going to assume, as in the case of " IF a", the expression ABS(a>5), is also a True and False decision for BASIC, why then if "a" is in fact greater than 5 (being a True statement) is not equal to -1  ??? . This comes back to what Steve was pointing out , that TRUE can be any value except zero. Zero is reserved for FALSE. But in my mind, the math to ABS(a>5) does work out to zero? Why it's calculating to 1 has me stumped. If the variable "a" has not been assigned a value prior to the call to calculate the expression, then it should equate to zero. So we have ABS(0>5) and zero is not greater than 5. Why it calculates to 1 and then determines 1 is TRUE because it not zero

It is only when we start plugging in values for "a" that @ 5 and under we get zero (False) and 6 and up we get 1 (allegedly TRUE), that your formula shines, and thank you for this tip on a work around for IF avoidance. It's just that naked expression ABS(a>5), where no prior value was given for "a" and the naked expression "IF a", where I'm not yet seeing the light. In my mind, those naked expressions should be either -1 or 0 per the conventional values for True and False

 




Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: True and False
« Reply #11 on: February 10, 2022, 11:00:40 am »
ABS (a > 5)

Remember, with the above, you evaluate what's in parentheses first.

If a is greater than 5, the result is -1. (TRUE)

If a is equal to, or less than 5, the result is 0. (FALSE)

ABS(-1) = 1  -- if a is greater than 5
ABS(0) = 0 -- a less than or equal to 5

So b = b + ABS(a > 5) will only increment the value of b when a is greater than 5.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
Re: True and False
« Reply #12 on: February 10, 2022, 11:01:24 am »
It's a lot simpler than you think!

0 is false, and anything that is not 0 is TRUE.

Between the IF and THEN statements, or the condition of the DO-LOOP loop, it does not actually examine a condition, but performs the operation there. If the result of the operation is not zero, i.e. TRUE, it executes the instructions after THEN.

Try !

IF 6> 5 THEN PRINT "TRUE"

and this:

OPERATION = 6> 5
PRINT OPERATION
IF OPERATION THEN PRINT "TRUE"

'OPERATION' will be 0 if the statement is false and -1 if true.
If you start from that, it's easier to see.

"OPERATION =" try several statements and you will understand everything!

operation = 0
operation = -1
operation = 100
operation = (2 and 2)

And such!

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
Re: True and False
« Reply #13 on: February 10, 2022, 11:35:44 am »
I want to write one more thought that used to confuse me.

IF 6 THEN PRINT "TRUE"

This is okay. 6 is not 0, so it is TRUE, so it executes.

IF 1 THEN PRINT "TRUE"

That's fine too, since 1 isn't 0 either, so it's TRUE.
Both statements are TRUE.

But!
IF 6 AND 1 THEN PRINT "TRUE"
However, it will not do this because 6 AND 1 = 0, so FALSE.

NEVER FORGET ! AND / OR / XOR is a mathematical, logical operation! If there is one in a statement, the program will work with it in a mathematical way, not in a literal interpretation!
Therefore, in such cases, the IF must be supplemented for it to work!
IF SGN (6) AND SGN (1) THEN PRINT "TRUE"

Offline Dimster

  • Forum Resident
  • Posts: 500
Re: True and False
« Reply #14 on: February 10, 2022, 11:54:08 am »
@MasterGy ... that's very interesting and helpful

So, in your first example you have " IF 6 > 5 then Print "True" and sure enough that prints "TRUE" . Why does the reverse of that equation not print FALSE?
IE "IF 6 < 5 then Print "FALSE"