True and False variables

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

Postby nkk_kan » Apr 16, 2008 22:07

@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:

Postby notthecheatr » Apr 17, 2008 0:58

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:

Postby Eclipzer » Oct 29, 2008 17:46

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
Site Admin
Posts: 3210
Joined: Jul 28, 2005 14:45
Location: Germany

Postby dkl » Oct 29, 2008 20:09

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
Site Admin
Posts: 6190
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Oct 29, 2008 20:20

Just to point out that this thread is over half a year old...

Also, be careful when you write things like NOT(expr). NOT isn't a function but a unary operator, and it can capture things outside the brackets. E.g. NOT(0) + 1 is equivalent to "NOT (0 + 1)", even though it looks like "(NOT 0) + 1".

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 5 guests