Boolean

From QB64 Phoenix Edition Wiki
Revision as of 21:03, 14 December 2022 by RhoSigma (talk | contribs)
Jump to navigation Jump to search

'Boolean statements are numerical evaluations that return true (-1 or NOT 0) or false (0) values that can be used in other arithmetic or bitwise logical operations.


Basic Returns

  • True evaluations return -1. NOT 0 = -1 in Basic. Can be used to increment a value.
  • 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!
  • 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.


Truth table of the BASIC Logical Operators

               Table 4: The logical operations and its results.

       In this table, A and B are the Expressions to invert or combine.
              Both may be results of former Boolean evaluations.
  ┌────────────────────────────────────────────────────────────────────────┐
  │                           Logical Operations                           │
  ├───────┬───────┬───────┬─────────┬────────┬─────────┬─────────┬─────────┤
  │   ABNOT BA AND BA OR BA XOR BA EQV BA IMP B │
  ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤
  │ truetrue  │ false │  true   │ true   │  false  │  true   │  true   │
  ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤
  │ truefalse │ true  │  false  │ true   │  true   │  false  │  false  │
  ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤
  │ falsetrue  │ false │  false  │ true   │  true   │  false  │  true   │
  ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤
  │ falsefalse │ true  │  false  │ false  │  false  │  true   │  true   │
  └───────┴───────┴───────┴─────────┴────────┴─────────┴─────────┴─────────┘
   Note: In most BASIC languages incl. QB64 these are bitwise operations,
         hence the logic is performed for each corresponding bit in both
         operators, where true or false indicates whether a bit is set or
         not set. The outcome of each bit is then placed into the respective
         position to build the bit pattern of the final result value.

   As all Relational Operations return negative one (-1, all bits set) for
    true and zero (0, no bits set) for false, this allows us to use these
    bitwise logical operations to invert or combine any relational checks,
    as the outcome is the same for each bit and so always results into a
            true (-1) or false (0) again for further evaluations.


Examples

Example 1
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


Example 2: Moving an ASCII character using the arrow keys and boolean statements to determine the new coordinate.

SCREEN 12
COLOR 7
LOCATE 11, 20: PRINT "Using Screen 12 here to be in 80 X 30 coordinates mode"
LOCATE 13, 6: PRINT "Simple Example of Alternative programming without IF-THEN-ELSE Statements"
LOCATE 15, 1: PRINT "Use the four Cursor keys to move the yellow cursor, text will not be disturbed"
LOCATE 17, 12: PRINT "When you END the program with the ESC key, cursor will disappear"

cordx% = 40
cordy% = 15

DO
oldcordx% = cordx%
oldcordy% = cordy%
p% = SCREEN(cordy%, cordx%) 'get ASCII character code at present position
COLOR 14: LOCATE cordy%, cordx%: PRINT CHR$(178); 'print cursor character to position

WHILE cordx% = oldcordx% AND cordy% = oldcordy% AND k$ <> CHR$(27)
k$ = INKEY$
cordx% = cordx% + (k$ = (CHR$(0) + "K") AND cordx% > 1) + ABS(k$ = (CHR$(0) + "M") AND cordx% < 80)
cordy% = cordy% + (k$ = (CHR$(0) + "H") AND cordy% > 1) + ABS(k$ = (CHR$(0) + "P") AND cordy% < 30)
WEND

COLOR 7: LOCATE oldcordy%, oldcordx%: PRINT CHR$(p%); 'replace overwritten screen characters

LOOP UNTIL k$ = CHR$(27)
Code by AlgoreIthm


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link