QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Kiara87 on October 08, 2020, 09:23:18 am

Title: instruction boolean QB64 true o false
Post by: Kiara87 on October 08, 2020, 09:23:18 am
Hello everyone
in the basic you can create a boolean statement where if the answer is true it returns the value 1 instead if the answer is false it returns the value 0
in my program even if the answers are true it returns 0 where is that wrong?

Code: QB64: [Select]
  1. DEFLNG A-Z
  2.  
  3. ' Main
  4. DIM Eta
  5. DIM Citta$
  6. DIM EsitoControllo
  7.  
  8. PRINT " = chiedi inserisci eta della persona"
  9. INPUT Eta
  10. PRINT "inserisci la città "
  11. INPUT Citta$
  12. EsitoControllo = Citta$ = "milano" AND (Eta >= 18) = 1
  13. PRINT "la persona e maggiorenne è anche di milano " + STR$(EsitoControllo)
  14.  
Title: Re: instruction boolean QB64 true o false
Post by: FellippeHeitor on October 08, 2020, 10:22:56 am
True is -1:

Code: QB64: [Select]
  1. DEFLNG A-Z
  2.  
  3. ' Main
  4. DIM Eta
  5. DIM Citta$
  6. DIM EsitoControllo
  7.  
  8. PRINT " = chiedi inserisci eta della persona"
  9. INPUT Eta
  10. PRINT "inserisci la città "
  11. INPUT Citta$
  12. EsitoControllo = (Citta$ = "milano") AND (Eta >= 18)
  13. PRINT "la persona e maggiorenne è anche di milano " + STR$(EsitoControllo)
  14.  
Title: Re: instruction boolean QB64 true o false
Post by: bplus on October 08, 2020, 11:09:26 am
Further proof: True = NOT 0

Code: QB64: [Select]
  1. PRINT NOT 0  
  2.  
Title: Re: instruction boolean QB64 true o false
Post by: Kiara87 on October 08, 2020, 04:21:55 pm
True is -1:

Code: QB64: [Select]
  1. DEFLNG A-Z
  2.  
  3. ' Main
  4. DIM Eta
  5. DIM Citta$
  6. DIM EsitoControllo
  7.  
  8. PRINT " = chiedi inserisci eta della persona"
  9. INPUT Eta
  10. PRINT "inserisci la città "
  11. INPUT Citta$
  12. EsitoControllo = (Citta$ = "milano") AND (Eta >= 18)
  13. PRINT "la persona e maggiorenne è anche di milano " + STR$(EsitoControllo)
  14.  

thank you   FellippeHeitor

I didn't know what true it was
and false = 0
and what instruction can I use to make me print true or false instead of 0 or -1

I have to print if it gives me -1 I have to print true
if instead it is 0 I have to print false

do i think it is possible?
Title: Re: instruction boolean QB64 true o false
Post by: Kiara87 on October 08, 2020, 04:23:05 pm
Further proof: True = NOT 0

Code: QB64: [Select]
  1. PRINT NOT 0  
  2.  

thanks bplus

but I wanted to print true
and print false
Title: Re: instruction boolean QB64 true o false
Post by: Dimster on October 08, 2020, 04:27:49 pm
a = 10
b = 5
if a > b then print "True" else print "False"
Title: Re: instruction boolean QB64 true o false
Post by: bplus on October 08, 2020, 05:20:56 pm
Code: QB64: [Select]
  1. ' you must set True and False variables to integer values
  2. False = 0
  3. True = NOT False
  4.  
  5. IF 5 < 15 THEN PRINT True ELSE PRINT False
  6.  
  7.  
  8.  
Title: Re: instruction boolean QB64 true o false
Post by: SMcNeill on October 08, 2020, 05:36:43 pm
Or maybe this is what you want:

Code: QB64: [Select]
  1. DIM Truth(-1 TO 0) AS STRING
  2. Truth(-1) = “TRUE”
  3. Truth(0) = “FALSE”
  4.  
  5. PRINT Truth(5 < 15)
  6. PRINT Truth(5 > 15)
  7.  

I’m thinking that should work.  If not, you’d just need to set a temp variable to hold the result first, before referencing it with your array.

Result = 5 < 15
PRINT Truth(Result)
Title: Re: instruction boolean QB64 true o false
Post by: TempodiBasic on October 08, 2020, 07:07:09 pm
Ho kiara87
See nearer the answer of Fellippe
There Is a clear transformation from your line 12 to his line 12.
In BASIC = Is both association like a =5, both comparison like  in IF a = b THEN ....., When you do multiple assignment in the same line of code you must use parentesis.
Title: Re: instruction boolean QB64 true o false
Post by: bplus on October 08, 2020, 07:31:50 pm
Or maybe this is what you want:

Code: QB64: [Select]
  1. DIM Truth(-1 TO 0) AS STRING
  2. Truth(-1) = “TRUE”
  3. Truth(0) = “FALSE”
  4.  
  5. PRINT Truth(5 < 15)
  6. PRINT Truth(5 > 15)
  7.  

I’m thinking that should work.  If not, you’d just need to set a temp variable to hold the result first, before referencing it with your array.

Result = 5 < 15
PRINT Truth(Result)



Oh hey how bout this!
Code: QB64: [Select]
  1. PRINT TF$(5 < 15)
  2. PRINT TF$(15)
  3. PRINT TF$(-15)
  4. PRINT TF$(20 - 10 = 10)
  5. PRINT TF$(1000 \ 100 - 10)
  6. PRINT TF$(1000 \ 100 - 10 = 0)
  7.  
  8. FUNCTION TF$ (numberOrExpression)
  9.     IF numberOrExpression = 0 THEN TF$ = "False" ELSE TF$ = "True"
  10.  


Title: Re: instruction boolean QB64 true o false
Post by: Richard Frost on October 09, 2020, 12:34:28 am
Because I like to play with fire, I've defined TRUE to be 1 in my current program.
I forget why it seemed like a good idea.  Probably because I think it's cool (and
short) to flip the status with var = var XOR 1.   

Why it's playing with fire is that I must keep in mind that Boolean expressions
will still evaluate to -1.

Title: Re: instruction boolean QB64 true o false
Post by: bplus on October 09, 2020, 10:50:29 am
Because I like to play with fire, I've defined TRUE to be 1 in my current program.
I forget why it seemed like a good idea.  Probably because I think it's cool (and
short) to flip the status with var = var XOR 1.   

Why it's playing with fire is that I must keep in mind that Boolean expressions
will still evaluate to -1.

For that I like
Code: QB64: [Select]
  1. toggle = 1 - toggle
I think I relearned from you!

and with that you don't mess with the Truth!
Title: Re: instruction boolean QB64 true o false
Post by: SMcNeill on October 09, 2020, 11:47:42 am
For that I like
Code: QB64: [Select]
  1. toggle = 1 - toggle
I think I relearned from you!

and with that you don't mess with the Truth!

And there’s always:

DIM b AS _UNSIGNED _BIT
PRINT b
b = NOT b
PRINT b
b = NOT b
PRINT b
Title: Re: instruction boolean QB64 true o false
Post by: Kiara87 on October 09, 2020, 04:59:24 pm
yes this is of type a boolean variable
thanks

Or maybe this is what you want:

Code: QB64: [Select]
  1. DIM Truth(-1 TO 0) AS STRING
  2. Truth(-1) = “TRUE”
  3. Truth(0) = “FALSE”
  4.  
  5. PRINT Truth(5 < 15)
  6. PRINT Truth(5 > 15)
  7.  

I’m thinking that should work.  If not, you’d just need to set a temp variable to hold the result first, before referencing it with your array.

Result = 5 < 15
PRINT Truth(Result)
Title: Re: instruction boolean QB64 true o false
Post by: Kiara87 on October 09, 2020, 05:03:09 pm
si ho notato nella riga 12 le parentesi che racchiudono
adesso mi è piu' chiaro
grazie

Ho kiara87
See nearer the answer of Fellippe
There Is a clear transformation from your line 12 to his line 12.
In BASIC = Is both association like a =5, both comparison like  in IF a = b THEN ....., When you do multiple assignment in the same line of code you must use parentesis.
Title: Re: instruction boolean QB64 true o false
Post by: _vince on October 09, 2020, 08:38:10 pm
Here is a fun tip: if you want to toggle a variable between two values 'a' and 'b' then xor it by (a xor b),

z = 7
z = z xor (7 xor 19) '=19
z = z xor (7 xor 19) '=7
z = z xor (7 xor 19) '=19
z = z xor (7 xor 19) '=7
'... and so on

Code: [Select]
z = 7 'start value
a = 0

for i=1 to 10
? chr$(asc("T")*-(a<>0)+asc("F")*-(a=0))+chr$(asc("R")*-(a<>0)+asc("A")*-(a=0))+chr$(asc("U")*-(a<>0)+asc("L")*-(a=0))+chr$(asc("E")*-(a<>0)+asc("S")*-(a=0))+chr$(asc("E")*-(a=0)), a, z


z = z xor (7 xor 19)
a = a xor (0 xor not 0)
next
Title: Re: instruction boolean QB64 true o false
Post by: Richard Frost on October 09, 2020, 10:55:18 pm
var XOR 1 is faster, in theory, than var = 1 - var,
because only one register is involved. 

Vince's toggle method is neat, and new to me.  It
even works for flipping between 0 and 1.  But it
does incur 3 register loads and 2 register operations.

Speed doesn't much matter for normal programs, but
it sure does for things like chess, lest the user die of
old age before a move is made.
Title: Re: instruction boolean QB64 true o false
Post by: OldMoses on October 10, 2020, 07:58:04 pm
Vince's toggle method is neat, and new to me.  It
even works for flipping between 0 and 1.  But it
does incur 3 register loads and 2 register operations.

Vince does cool stuff, am I right that the "TRUE" / "FALSE" is a example of "branchless" programming?