Operators and Expressions
This chapter provides an overview of expressions and operators within Caché Basic.
Expressions
An expression is a combination of keywords, operators, variables, and constants that yield a string, number, or object value. An expression can perform a calculation, manipulate characters, or test data.
Operators
Caché Basic includes a variety of built-in operators.
Operator Precedence
When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence. Parentheses can be used to override the order of precedence and force some parts of an expression to be evaluated before other parts. Operations within parentheses are always performed before those outside. Within parentheses, however, normal operator precedence is maintained.
When expressions contain operators from more than one category, arithmetic operators are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last. Comparison operators all have equal precedence; that is, they are evaluated in the left-to-right order in which they appear. Arithmetic and logical operators are evaluated in the following order of precedence:
Arithmetic | Comparison | Logical |
---|---|---|
Exponentiation (^) | Equality (=) | Not |
Negation (-) | Inequality (<>) | And |
Multiplication and division (*, /) | Less than (<) | Or |
Integer division (\) | Greater than (>) | Xor |
Modulus arithmetic (Mod) | Less than or equal to (<=) | Eqv |
Addition and subtraction (+, -) | Greater than or equal to (>=) | Imp |
String concatenation (&) | Is | & |
When multiplication and division occur together in an expression, each operation is evaluated as it occurs from left to right. Likewise, when addition and subtraction occur together in an expression, each operation is evaluated in order of appearance from left to right.
The string concatenation operator (&) is not an arithmetic operator, but in precedence it does fall after all arithmetic operators and before all comparison operators. The Is operator is an object reference comparison operator. It does not compare objects or their values; it checks only to determine if two object references refer to the same object.
Comparison Operators
The Is operator has specific comparison functionality that differs from the operators in the following table. The following table contains a list of the comparison operators and the conditions that determine whether result is True or False:
Operator | Description | True if | False if |
---|---|---|---|
< | Less than | expression1 < expression2 | expression1 >= expression2 |
<= | Less than or equal to | expression1 <= expression2 | expression1 > expression2 |
> | Greater than | expression1 > expression2 | expression1 <= expression2 |
>= | Greater than or equal to | expression1 >= expression2 | expression1 < expression2 |
= | Equal to | expression1 = expression2 | expression1 <> expression2 |
<> | Not equal to | expression1 <> expression2 | expression1 = expression2 |
When comparing two expressions, you may not be able to easily determine whether the expressions are being compared as numbers or as strings.
The following table shows how expressions are compared or what results from the comparison, depending on the underlying subtype:
If | Then |
---|---|
Both expressions are numeric | Perform a numeric comparison. |
Both expressions are strings | Perform a string comparison. |
One expression is numeric and the other is a string | The numeric expression is less than the string expression. |