XOR (boolean): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
{{KW|XOR (boolean)|XOR}} evaluates two conditions and if either of them is True then it returns True, if both of them are True then it returns False, if both of them are False then it returns False.
[[XOR (boolean)|XOR]] evaluates two conditions and if either of them is True then it returns True, if both of them are True then it returns False, if both of them are False then it returns False.




{{PageSyntax}}
{{PageSyntax}}
: {{Parameter|condition}} {{KW|XOR (boolean)|XOR}} {{Parameter|condition2}}
: {{Parameter|condition}} [[XOR (boolean)|XOR]] {{Parameter|condition2}}




{{PageDescription}}
{{PageDescription}}
* Either {{Parameter|condition}} or {{Parameter|condition2}} must be True for the evaluation to return True.
* Either {{Parameter|condition}} or {{Parameter|condition2}} must be True for the evaluation to return True.
* It is called '''"exclusive OR"''' because the conditions cannot both be True for it to return True like the {{KW|OR (boolean)|OR}} evaluation.
* It is called '''"exclusive OR"''' because the conditions cannot both be True for it to return True like the [[OR (boolean)|OR]] evaluation.
* {{Parameter|condition}} and {{Parameter|condition2}} can themselves contain XOR evaluations.
* {{Parameter|condition}} and {{Parameter|condition2}} can themselves contain XOR evaluations.



Latest revision as of 01:00, 25 January 2023

XOR evaluates two conditions and if either of them is True then it returns True, if both of them are True then it returns False, if both of them are False then it returns False.


Syntax

condition XOR condition2


Description

  • Either condition or condition2 must be True for the evaluation to return True.
  • It is called "exclusive OR" because the conditions cannot both be True for it to return True like the OR evaluation.
  • condition and condition2 can themselves contain XOR evaluations.


         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: Dilemma...


True = NOT False
AndersWon = True
PeterWon = True
IF AndersWon = True XOR PeterWon = True THEN
PRINT "Thank you for your honesty!"
ELSE
PRINT "You can't both have won (or lost)!"
END IF

You can't both have won (or lost)!


See also



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