Expressions

Prior | Next

Expressions are made up of operators and operands.  Most ESPL operators are binary; they take two operands. The rest are unary and take only one operand. Binary operators use the usual algebraic form (for example, A + B). A unary operator always precedes its operand (for example, -B).

ESPL uses operators in a very natural, well-established, and intuitive manner. Both mathematicians and other computer languages tend to treat them in a very similar manner. In more complex expressions, rules of precedence clarify the order in which operations are performed.

 

Operators                     Precedence     Categories
not                           first (high)   unary operators
*,/, div, mod, and, shl, shr  second         multiplying operators
+,-, or, xor                  third          adding operators
=, <>, <, >, <=, >=           fourth (low)   relational operators
Precedence
There are three basic rules of precedence:
  • An operand between two operators of different precedence is bound to the operator with higher precedence.
  • An operand between two equal operators is bound to the one on its left.
  • Expressions within parentheses are evaluated prior to being treated as a single operand. You can use parentheses to force an addition to occur before a multiplication.

Operations with equal precedence are performed from left to right. Examples:

  writeln( 2 + 5 * 6 ); {prints 32, multiplication is done before addition because multiplication has higher precedence}
  writeln( (2+5) * 6 ); {prints 42, parentheses are evaluated and then multiplied by 6}
  writeln( 2 * 8 div 5 ); {prints 3, 2 * 8 = 16. 16 div 5 = 3}
  writeln( 2 * ( 8 div 5 ) ); {prints 2. 8 div 5 = 1. 1 * 2 = 2}

Mod Operator
The mod operator returns the remainder obtained by dividing its two operands; that is, K mod J = K - (K div J) * J. The sign of the result of mod is the same as the sign of K. A run-time error occurs if J is zero.

Shift Operators
The operations K shl J and K shr J shift the value of K to the left or right by J bits.

Relational Operators
When the operands =, <>, <, >, >=, and <= are of simple types, they must be compatible types; however, if one operand is of a real type, the other can be of an integer type. The relational operators compare strings according to the ordering of the extended ASCII character set.

Prior | Next