Boolean

From QB64 Wiki

Revision as of 04:34, 1 November 2011 by Clippy (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Boolean statements return True (-1) or False (0) numerical evaluations.



Basic Returns:
  • True evaluations return -1.
  • For positive True results, subtract it, multiply it by a negative value or use ABS.
  • False evaluations return 0. Watch out for "Division by 0" errors!


Relational Operators:
  • > is Greater than
  • < is Less than
  • = is Equal to
  • >= is Greater than or Equal to
  • <= is Less than or Equal to
  • <> is Not Equal to
  • When evaluating a True value, an IF value < 0 statement is NOT necessary for return values not 0.


Boolean Conditional Operators:
  • AND can be used to add extra conditions to a boolean statement evaluation. Both must be True.
  • OR can be used to add alternate conditions to a boolean statement evaluation. One must be True.
  • Parenthesis are allowed inside of boolean statements to clarify an evaluation.
  • Note that Basic returns -1 for True and 0 for False.


Example: Using 2 different boolean evaluations to determine a leap year.

INPUT "Enter a year greater than 1583: ", annum$ Y = VAL(annum$) leap1 = (Y MOD 4 = 0 AND Y MOD 100 <> 0) OR (Y MOD 400 = 0) leap2 = (Y MOD 4 = 0) - (Y MOD 100 = 0) + (Y MOD 400 = 0) PRINT "Year = "; annum$, "Leap1 = "; leap1, "Leap2 = "; leap2

Explanation: Both boolean evaluations will return -1 if the year is a leap year. It is not simply every four years as many people think. That is checked by the first evaluation (Y MOD 4 = 0) of each. In new century years like 1900 (which was not a leapyear) there is only one leap year every 400 years. 100 is used with MOD to see if there is a remainder. When that is true, the boolean return of that part of the first evaluation will be 0. The second returns -1 (which is actually added). In both evaluations the result of (Y MOD 400 = 0) indicates a century leap year.


Entry year = 2000:
leap1 = (-1 AND 0) OR -1 = -1 ' the AND evaluation returns False(0) so the OR value is used.
leap2 = (-1) - (-1) + (-1) = -1 + 1 + -1 = -1


Entry year = 1900:
leap1 = (-1 AND 0) OR 0 = 0 OR 0 = 0
leap2 = (-1) - (-1) + (0) = -1 + 1 + 0 = 0


See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page