Relational Operations: Difference between revisions
No edit summary |
No edit summary |
||
Line 67: | Line 67: | ||
* [[IF...THEN]] | * [[IF...THEN]] | ||
* [[DO...LOOP]], [[WHILE...WEND]] | * [[DO...LOOP]], [[WHILE...WEND]] | ||
* [[ | * [[CASE IS]], [[NOT]] | ||
* [[AND]], [[OR]], [[XOR]], [[EQV]], [[IMP]] | * [[AND]], [[OR]], [[XOR]], [[EQV]], [[IMP]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 15:23, 25 September 2024
QB64 supports several relational operations, which are binary operations that test numeric or string values and return an INTEGER value representing a boolean true (-1) or false (0) result. These operations are primarily used in expressions where a condition is required, such as the IF...THEN statement.
List of relational 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 │ ├────────────┬───────────────────────────────────────────┬────────────────┤ │ Operation │ Description │ Example 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. |
Comparing numerical values and variables
For numeric operands, such as INTEGER or DOUBLE values, the relational operations behave as one would expect. For example, the expression (-3.4 < 1.2) evaluates to true, and the expression (50 = 100) evaluates to false.
Example: When a user enters a value greater than or equal to 5, the boolean statement returns -1 . Zero is printed otherwise.
INPUT "Enter a value from 1 to 10: ", number PRINT number >= 5 |
Comparing string values and variables
For string operands, the = and <> operators test, respectively, if the operands are of the same or differing length, and that each of the corresponding characters match or don't match. For example, the three expressions ("abc" = "abc"), ("abc" <> "abd") and ("abc" <> "abcdef") all evaluate to true.
When two strings are compared, the left string sets the number of characters to evaluate when not checking for equality. The < and > operators find the first set of non-matching characters and test if the ASCII code values of those characters are less than or greater than each other, respectively. Equal strings MUST be of the same length with identical cased letters or ASCII characters!
If one string is identical to part of the other, < returns false if the left-hand side string is shorter than the right-hand side string, while > returns true if the left-hand side string is longer than the right-hand side string. For example, the expressions ("abc" < "abd") and ("abcdef" > "abc") both evaluate to true. Even space ASCII character values are evaluated!
Example: Shows that the left hand string sets the number of characters to evaluate when using > or < and are not equal when longer.
PRINT "abc" < "abcd" PRINT "abc" = "abc" PRINT "abc" = "abcd" PRINT "abcd" > "abc" |
0 -1 0 -1 |
Comparing user-defined type variables
Variables of a user-defined type (UDT) cannot be used as operands to the relational operators, but numeric or string type fields can be used as described above. For example:
- TYPE T
- a AS INTEGER
- END TYPE
- TYPE T
- TYPE U
- b AS INTEGER
- END TYPE
- TYPE U
- DIM x AS T : x.a = 10
- DIM y AS U : y.b = 20
- PRINT x < y ' <- error: type mismatch
- PRINT x.a < y.b ' <- outputs "-1"
Boolean values
The INTEGER values for true and false are such that the bitwise Logical Operators, such as NOT, AND and OR can be used to invert and combine test results. For example, the expression (NOT (10 >= 20)) evaluates to true, while the expression ((2 < 1) AND ("three" = "four")) evaluates to false.
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 │ ├───────┬───────┬───────┬─────────┬────────┬─────────┬─────────┬─────────┤ │ A │ B │ NOT B │ A AND B │ A OR B │ A XOR B │ A EQV B │ A IMP B │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ true │ true │ false │ true │ true │ false │ true │ true │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ true │ false │ true │ false │ true │ true │ false │ false │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ false │ true │ false │ false │ true │ true │ false │ true │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ false │ false │ 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. |
See also