Operators
This section describes the various operators HB++ offers.
Precedence and Associativity
The precedence of an operator defines its priority with respect to the other operators. It ensures that an expression like 2+3*4 is always evaluated in the same way, by defining for example that multiplication must always take place before addition.
The associativity of an operator defines the order of evaluation in an expression made up of operators of the same priority. It ensures that an expression like 2^3^4 is always evaluated in the same way, by defining for example that the evaluation must always take place from right to left.
Both precedence and associativity can be modified by the use of parenthesis in an expression. The table below lists the characteristics of the available operators from lowest to highest priority :
| Operators | Arity | Associativity |
| Or Xor | Binary | Left to right |
| And | Binary | Left to right |
| Not | Unary | Right to left |
| = <> < > <= >= Is | Binary | Left to right |
| & | Binary | Right to left |
| Shl Shr | Binary | Right to left |
| + - | Binary | Left to right |
| Mod | Binary | Left to right |
| * / | Binary | Left to right |
| ^ | Binary | Right to left |
| + - | Unary | Right to left |
| IIf | Ternary | n/a |
| New | n/a | n/a |
Operands Type
Apart from special cases, binary operators require the two operands to be the same type. If this is not the case, the HB++ compiler tries to convert the less precise operand into the more precise type by implicitly calling one of the type conversion functions. Thus, conversion errors can arise at runtime.
Besides, the majority of operators behave differently depending on the operand type. For example, the division operator / provides a decimal result only if at least one of the operands is a Single or Double, and an integer if this is not the case. The logical operators And, Or, Xor and Not act differently on Boolean values than on Byte, Integer, or Long values.
You should always pay attention to the expression types provided to the operators, if necessary by calling type conversion functions, to make sure that the operation is carried out in the way you require.
Order of evaluation
Order of evaluation always conforms to the associativity of the operators the expression is made of. For example, in the following expressions, it is guaranteed that MyFunc1 will be called before MyFunc2 and that MyFunc3 will be called after MyFunc4:
x = MyFunc1() + MyFunc2() x = MyFunc3() & MyFunc4()
Evaluation of boolean expressions stops as soon as the result is known. For example, in the following expressions, the call to MyFunc3 is dropped if MyFunc1 returned True or if MyFunc2 returned False, as its value is not necessary to evaluate the expression:
x = MyFunc1() Or MyFunc3() x = MyFunc2() And MyFunc3()
The compiler can also drop useless parts of an expression at compile-time, especially if optimizations are turned on. For example, the following expressions all evaluate to zero whetever the value returned by the call to MyFunc; therefore, the call to this function is skipped:
x = 0 * MyFunc() x = MyFunc() Shl 43 x = MyFunc() And 0
If you really need an expression to be fully evaluated, you can enclose it in a call to the Strict pseudo-function. This tells the compiler to locally disable all optimizations, and to generate code that corresponds as precisely as possible to the enclosed expression.
Reference
The collection of operators described in this chapter is summarized below.
| Operator | Description |
| Compares two expressions. | |
| - | Subtracts two expressions. |
| & | Concatenates two strings. |
| * | Multiplies two expressions. |
| / | Divides two expressions. |
| ^ | Raises to a power. |
| + | Adds to expressions. |
| And | Perform a logical conjunction on two expressions. |
| IIf | Returns one of two expressions, depending on the evaluation of a conditional expression. |
| Is | Compares two object references. |
| Mod | Returns the remainder of the division of two expressions. |
| New | Creates a new instance of the specified class. |
| Not | Performs a logical negation on an expression. |
| Or | Performs a logical disjunction on two expressions. |
| Shl | Bitwise shift left of an integer value. |
| Shr | Bitwise shift right of an integer value. |
| Strict | Locally disable all optimizations for expression evaluation. |
| Xor | Performs a logical exclusion on two expressions. |