AND (boolean): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Protected "AND (boolean)" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite)))
No edit summary
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
The [[AND (boolean)|AND]] conditonal operator is used to include another evaluation in an [[IF...THEN]] or [[Boolean]] statement.
The [[AND (boolean)|AND]] conditonal operator is used to include another evaluation in an [[IF...THEN]] or [[Boolean]] statement.




Line 16: Line 15:




{{Template:RelationalTable}}
{{RelationalOperationsPlugin}}




Line 36: Line 35:


''Example:'' Using a AND a more complex way.
''Example:'' Using a AND a more complex way.
{{CodeStart}} '' ''
{{CodeStart}}
a% = 100
a% = 100
b% = 50
b% = 50
Line 47: Line 46:
{{Cl|ELSE}}
{{Cl|ELSE}}
{{Cl|PRINT}} "False"
{{Cl|PRINT}} "False"
{{Cl|END IF}} '' ''
{{Cl|END IF}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 56: Line 55:


{{PageSeeAlso}}
{{PageSeeAlso}}
* [[AND]], [[OR]] {{text|(logical operators)}}
* [[AND]], [[OR]] {{Text|(logical operators)}}
* [[OR (boolean)]], [[XOR (boolean)]]
* [[OR (boolean)]], [[XOR (boolean)]]
* [[IF...THEN]]
* [[IF...THEN]]

Latest revision as of 22:11, 11 February 2023

The AND conditonal operator is used to include another evaluation in an IF...THEN or Boolean statement.


Syntax

IF condition AND condition2


Description

  • If condition AND condition2 are true then the evaluation returns true (-1).
  • condition and condition2 can also contain their own AND evaluations.
  • Both the IF evaluation and the AND evaluation must be true for the statement to be true.
  • Statements can use parenthesis to clarify an evaluation.
  • AND (boolean) and OR (boolean) cannot be used to combine command line operations.
  • Not to be confused with the AND and OR numerical operations.


         Table 3: The relational operations for condition checking.

 In this table, A and B are the Expressions to compare. Both must represent
 the same general type, i.e. they must result into either numerical values
 or STRING values. If a test succeeds, then true (-1) is returned, false (0)
     if it fails, which both can be used in further Boolean evaluations.
 ┌─────────────────────────────────────────────────────────────────────────┐
 │                          Relational Operations                          │
 ├────────────┬───────────────────────────────────────────┬────────────────┤
 │ OperationDescriptionExample usage  │
 ├────────────┼───────────────────────────────────────────┼────────────────┤
 │   A = B    │ Tests if A is equal to B.                 │ IF A = B THEN  │
 ├────────────┼───────────────────────────────────────────┼────────────────┤
 │   A <> B   │ Tests if A is not equal to B.             │ IF A <> B THEN │
 ├────────────┼───────────────────────────────────────────┼────────────────┤
 │   A < B    │ Tests if A is less than B.                │ IF A < B THEN  │
 ├────────────┼───────────────────────────────────────────┼────────────────┤
 │   A > B    │ Tests if A is greater than B.             │ IF A > B THEN  │
 ├────────────┼───────────────────────────────────────────┼────────────────┤
 │   A <= B   │ Tests if A is less than or equal to B.    │ IF A <= B THEN │
 ├────────────┼───────────────────────────────────────────┼────────────────┤
 │   A >= B   │ Tests if A is greater than or equal to B. │ IF A >= B THEN │
 └────────────┴───────────────────────────────────────────┴────────────────┘
   The operations should be very obvious for numerical values. For strings
   be aware that all checks are done case sensitive (i.e. "Foo" <> "foo").
   The equal/not equal check is pretty much straight forward, but for the
   less/greater checks the ASCII value of the first different character is
                          used for decision making:

   E.g. "abc" is less than "abd", because in the first difference (the 3rd
        character) the "c" has a lower ASCII value than the "d".

   This behavior may give you some subtle results, if you are not aware of
                   the ASCII values and the written case:

   E.g. "abc" is greater than "abD", because the small letters have higher
        ASCII values than the capital letters, hence "c" > "D". You may use
        LCASE$ or UCASE$ to make sure both strings have the same case.


Examples

Example: Using AND in an IF statement.


a% = 100
b% = 50

IF a% > b% AND a% < 200 THEN PRINT "True"

True

Explanation: Both condition evaluations must be true for the code to be executed.


Example: Using a AND a more complex way.

a% = 100
b% = 50
c% = 25
d% = 50
e% = 100

IF (a% > b% AND b% > c%) AND (c% < d% AND d% < e%) THEN
PRINT "True"
ELSE
PRINT "False"
END IF
True

Explanation: The evaluations in the paranteses are evaluated first then the evaluation of the paranteses takes place, since all evaluations return True the IF...THEN evaluation returns True. If any of the evaluations returned False then the IF...THEN evaluation would also return False.


See also



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