Author Topic: True and False  (Read 7387 times)

0 Members and 1 Guest are viewing this topic.

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: True and False
« Reply #15 on: February 10, 2022, 12:02:17 pm »
you think so, that's right! :)
You have to give it a try and you’ll see that it’s actually a simple thing.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: True and False
« Reply #16 on: February 10, 2022, 12:02:59 pm »
If statements work on truth conditions.

IF (whatever) THEN... only executes what comes next if (whatever) evaluates not equal to zero.

IF 6 < 5 THEN...

6 < 5 is false, so it evaluates to 0, which makes it become

IF 0 THEN...   <-- ZERO says the condition is false; don't execute what comes next.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: True and False
« Reply #17 on: February 10, 2022, 12:56:52 pm »
As in life, the search for Truth is often elusive. Thanks guys.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: True and False
« Reply #18 on: February 10, 2022, 01:22:45 pm »
I think where you might be having issues is with over thinking things.

Computers aren't LOGICAL things like people are -- they're MATHEMATICAL.

All these issues you're having can be resolved by breaking things down into 2 real processes.



First, is solving an equation until you get a singular result.

a < 5

The above is a math equation, just like 2 + 3, 6 * 4, or SQR(9) are.  In the case of a < 5, the equation can give us 2 results -- -1 if a is less than 5, 0 if it's greater than or equal.

<, >, = are all operators which compare values and return -1 or 0 as a result.

Want to test that?   Just:  PRINT a < 5, and toss various values to a.  The result is going to be -1 or 0.



Now, once you've gotten a singular result, that result determines whether we execute code after an IF statement.

IF (singular result) THEN ....

As long as that (singular result) is ANYTHING BUT ZERO, the code after the IF executes.  If the result is zero, we skip the next block of code.

IF 3 THEN...  <-- 3 isn't 0, so we execute what comes next.

IF 0 THEN...  we don't execute what comes next.



So in the case of "IF a < 6 THEN..."

If a is less than 6, that condition evaluates as:  a < 6 becomes -1.

Substitute that result in the statement and we get:  IF -1 THEN....

Since that final result is ANYTHING BUT ZERO, we execute that IF statement.



Now, in the case that a was greater than 6, it'd evaluate as: a < 6 becomes 0.

Substitute in the IF and we get:  IF 0 THEN...

Since the result is 0, we don't execute the code after that THEN.



Step one is: Resolve the formula to a single result.
Step two is: If that result is zero, skip the code after THEN.


And that's all that's really going on with your true/false checks.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline tomxp411

  • Newbie
  • Posts: 28
    • View Profile
Re: True and False
« Reply #19 on: February 10, 2022, 03:16:19 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.

You're over-complicating the IF statement.

The IF statement tests for zero. That's all it does. Any other comparisons are actually done by the expression in the IF statement, not IF itself.

IF <value> THEN
<value is not zero>
ELSE
<value is zero>
END IF

IF does not test for equality, greater, lesser, or anything else. It simply checks for zero. In fact, the machine code for this ends up as "Jump if Zero" (JZ) and "Jump if Not Zero" (JNZ).

So everything between IF and THEN is actually an expression., which returns a numeric result.

In BASIC, an expression is basically anything that can be turned into a value: "3" is an expression, "4  + 5" is an expression, and "A < Y" is an expression that returns a Boolean result.

And since a Boolean result is either On or Off, we treat Off as zero and On as -1 (Because signed binary integers are negative when the left bit is on. It's more complicated than that, but that's how the "negative" flag works in machine language.)

So things like IF X=Y are actually two separate operations: the expression (X=Y) is evaluated first and returned as a single value. The IF statement then jumps to the ELSE or END IF when the value is non-zero.

To understand Boolean expressions a bit more, consider these statements:

PRINT 0
PRINT 1
PRINT 0=0
PRINT 0=1
PRINT 1=1

PRINT 0=0 prints -1, because when the = is used in an expression, it's a test for equality. And a test for equality returns -1 when the two values are equal.
Likewise 0=1 returns 0, and 1=1 returns -1

Once you understand the separation between statement and expression, you'll better understand how to use the IF statement to write complex and useful tests.

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: True and False
« Reply #20 on: February 10, 2022, 03:24:52 pm »
You put it right SMcNeill!

I had similar thoughts, I just couldn’t express myself well.

"SOMETHING ="
After something, there can be functions, operators, relations, equality, whatever. Whatever. If the formula can be evaluated, 'SOMETHING' will have a result. 'IF' has only so much to do with the idea that it executes, if not 0. That's it. The possibilities offered by the programming language must be understood as ‘SOMETHING’ as it can be equal and what are the means by which we can express an idea, a calculation.
Actually, on my own ... I need to be able to play with it. In vain, let us put it according to our own thoughts. No need. You have to try. That’s why I wrote to Dimster to try, play with it, experiment, and when everything becomes clear, he realizes: it’s all simple! but I just complicated it!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: True and False
« Reply #21 on: February 10, 2022, 03:40:13 pm »
You guys are priceless. Very much appreciate shining a light on this for me.

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: True and False
« Reply #22 on: February 10, 2022, 04:09:14 pm »
I find this a terrific thread of discussion.  It makes for excellent documentation.

Do you think it makes sense for me, and/or is it okay, to have added a link to this discussion at the bottom of the Boolean wiki page ?

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: True and False
« Reply #23 on: February 10, 2022, 04:53:53 pm »
The beauty of programming. :) when a man grabs the heat and becomes a zombie and is already lazy to press the keyboard, he tries to simplify.

Task: Add all numbers divisible by 3 from 0 to 500 and all numbers divisible by 7 from 500 to 1000, except for square numbers. and except for 35!

Code: QB64: [Select]
  1. 'using IF
  2.  
  3. FOR x = 0 TO 1000
  4.     IF x <> 35 THEN
  5.         IF SQR(x) <> INT(SQR(x)) THEN
  6.             IF x < 500 THEN
  7.                 IF x / 5 = INT(x / 5) THEN sum = sum + x
  8.             ELSE
  9.                 IF x / 7 = INT(x / 7) THEN sum = sum + x
  10.             END IF
  11.         END IF
  12.     END IF
  13. PRINT sum
  14.  
  15. sum = 0
  16.  
  17. 'without IF
  18. FOR x = 0 TO 1000
  19.     sum = sum + ABS(x <> 35 AND SQR(x) <> INT(SQR(x))) * ABS((x < 500) * (x / 5 = INT(x / 5)) + (x > 500) * (x / 7 = INT(x / 7))) * x
  20. PRINT sum
  21.  
  22.  
  23.  

« Last Edit: February 10, 2022, 05:01:07 pm by MasterGy »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: True and False
« Reply #24 on: February 10, 2022, 05:10:24 pm »
The beauty of programming. :) when a man grabs the heat and becomes a zombie and is already lazy to press the keyboard, he tries to simplify.

Task: Add all numbers divisible by 3 from 0 to 500 and all numbers divisible by 7 from 500 to 1000, except for square numbers. and except for 35!

Code: QB64: [Select]
  1. 'using IF
  2.  
  3. FOR x = 0 TO 1000
  4.     IF x <> 35 THEN
  5.         IF SQR(x) <> INT(SQR(x)) THEN
  6.             IF x < 500 THEN
  7.                 IF x / 5 = INT(x / 5) THEN sum = sum + x
  8.             ELSE
  9.                 IF x / 7 = INT(x / 7) THEN sum = sum + x
  10.             END IF
  11.         END IF
  12.     END IF
  13. PRINT sum
  14.  
  15. sum = 0
  16.  
  17. 'without IF
  18. FOR x = 0 TO 1000
  19.     sum = sum + ABS(x <> 35 AND SQR(x) <> INT(SQR(x))) * ABS((x < 500) * (x / 5 = INT(x / 5)) + (x > 500) * (x / 7 = INT(x / 7))) * x
  20. PRINT sum
  21.  
  22.  
  23.  


Code: QB64: [Select]
  1. 'Task: Add all numbers divisible by 3 from 0 to 500 and all numbers divisible by 7 from 500 to 1000, except for square numbers. and except for 35!
  2.  
  3.  
  4. For i = 0 To 500
  5.     sum = sum + Div(i, 3)
  6. For i = 500 To 1000
  7.     sum = sum + Div(i, 7)
  8.  
  9. Print "SUM = "; sum
  10.  
  11. Function Div (num, divisor)
  12.     If Valid(num) And (num Mod divisor = 0) Then Div = num
  13.  
  14. Function Valid (num As Integer)
  15.     If Sqr(num) = Int(Sqr(num)) Then Exit Function
  16.     If num = 35 Then Exit Function
  17.     Valid = -1
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: True and False
« Reply #25 on: February 10, 2022, 05:29:19 pm »
This is it !!! ! beautiful ! compression and elegance! this is the attitude of programming! :)))))

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: True and False
« Reply #26 on: February 13, 2022, 12:04:57 pm »
Hey @MasterGy ... when I asked you why 6 < 5 did not print 0 (or False) and the explanation was revealed that the IF evaluation wouldn't go to any instructions after evaluating the first part to zero....well it seems if you use an ELSE in the IF statement you can get it to print the zero (or False)

If 6 > 5 then print 6 > 5 Else print 6 >5 ... this results in -1 printed to the screen
if 6 < 5 then print 6 < 5 Else print 6<5 ... this results in 0 printed to the screen
so could also ask it to print True or False and it works.

I'm going to start to incorporate this double choice in resultants and see if this helps or hinders. As I had mentioned, I seems to be getting a lot of miss reads especially on False. The explanation on how IF is actually looking for zero sure did turn a light on.