## True and False variables

New to FreeBASIC? Post your questions here.
nkk_kan
Posts: 209
Joined: May 18, 2007 13:01
Location: India
Contact:
@RJkouba :

It looks like your dagger variable is string data type from this statement

Code: Select all

`dagger(1-30)="You see a dagger on the floor"`

If data type of a constant is not defined then it checks the value given to it and infers a data type.'
here constants "false" and "true" are given values as integers so they become integer data type,
Thus,It shows that "Type Mismatch" and "Invalid Assignment" errors.

Code: Select all

`const s = "this is a string"const a = 10`

here, S will be made a string data type constant while a will become an integer.
[/code][/quote]
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:
TRUE and FALSE are nice ways to do things. TRUE is usually -1 or &hfffffff (add more f's depending on what data type you use) - in other words, NOT FALSE. That makes sense, since true should be the opposite of false and that's what NOT does. Thinking in terms of the logic operations helps this to make sense.

And thinking in terms of zero or non-zero is fine too - in some cases it's important, such as when an error code is returned - but that doesn't always work well, so it's preferable to use -1 or NOT(FALSE) in most cases.
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:
Here's a simpler program to demonstrate the logical errors that occur when using false=0, true=1 for boolean values:

Code: Select all

`  const TRUE =1  const FALSE=0      dim as integer answer=TRUE      if not answer then    ? "answer is false"  else    ? "correct is true"  end if      sleep`

Ideally this should return "correct is true" but it fails to do so since NOT answer, which is really NOT 1, evaluates to -2. Since all non-zero values will pass a conditional test, the "answer is false" is actually returned.
dkl
Posts: 3210
Joined: Jul 28, 2005 14:45
Location: Germany
I define my own TRUE as not(FALSE) indeed, which works fine together with FB.
However for example the Windows API uses 1 for TRUE. So, you got to be careful there.

And such things are the reason to choose testing against "<> FALSE" (FALSE is 0 everywhere) over "= TRUE".
counting_pine