HP 3000 Manuals

Evaluation of Expressions [ HP Business BASIC/XL Reference Manual ] MPE/iX 5.0 Documentation


HP Business BASIC/XL Reference Manual

Evaluation of Expressions 

HP Business BASIC/XL evaluates a simple (one operator) expression by
evaluating its operands or actual parameters from left to right, and then
performing the operation or function.

Examples 

     10 A=2
     20 B=7
     30 C=A+B
     99 END

In line 30 of the above program, HP Business BASIC/XL evaluates A and B
(in that order) and then adds their values (2 and 7, respectively) to
produce the result, 9.

     100 X=10
     110 Y=15
     120 Z=20
     130 Max_xyz=MAX(X,Y,Z)
     999 END

In line 130 of the above program, HP Business BASIC/XL evaluates the
expression MAX(X,Y,Z) by first evaluating X, Y, and Z (in that order) and
then comparing their values (10,15, and 20, respectively) and returning
the largest value (20).

More complex expressions can be constructed by substituting expressions
for the operands or parameters.  For example, the expressions A+B and
MAX(X,Y,Z) are operands of the addition operator in the expression
(A+B)+MAX(X,Y,Z). HP Business BASIC/XL evaluates (A+B)+MAX(X,Y,Z) by
first evaluating A+B and MAX(X,Y,Z) (in that order) as explained above,
and then adding their values (nine and 20, respectively) to produce the
result, 29.

When an expression has expressions for operands or parameters, operator
hierarchy determines the order in which the component operations are
performed.  The general rule of left to right expression evaluation
applies to the evaluation of each subexpression.  For example, operator
hierarchy dictates that the expression 2*3+4*5 is evaluated as (2*3) +
(4*5), where the expressions in parentheses are evaluated first.

Operator Hierarchy 

When an expression contains several operators, operator precedence is
used to determine the evaluation order.  The operator hierarchy
establishes the precedence relationship among the HP Business BASIC/XL
operators.  Expressions with operators of equal precedence are evaluated
from left to right.

Table 3-27 shows the HP Business BASIC/XL operator hierarchy.  An
operator takes precedence over those below it in the table.  Operators on
the same line of the table have equal precedence.

          Table 3-27.  Operator Hierarchy 

--------------------------------------------------------------------------------------------
|                                                     |                                    |
|            Operator or Operator Category            |             Symbol(s)              |
|                                                     |                                    |
--------------------------------------------------------------------------------------------
|                                                     |                                    |
| Subexpressions within Parentheses                   | ()                                 |
|                                                     |                                    |
--------------------------------------------------------------------------------------------
|                                                     |                                    |
| Exponentiation Operator *                           | **, ^                              |
|                                                     |                                    |
--------------------------------------------------------------------------------------------
|                                                     |                                    |
| Unary Operators                                     | +, -, NOT                          |
|                                                     |                                    |
--------------------------------------------------------------------------------------------
|                                                     |                                    |
| Multiplication and Division Operators               | *, MOD, /, DIV                     |
|                                                     |                                    |
--------------------------------------------------------------------------------------------
|                                                     |                                    |
| Addition and Subtraction Operators                  | +, -                               |
|                                                     |                                    |
--------------------------------------------------------------------------------------------
|                                                     |                                    |
| Minimum and Maximum Operators                       | MIN, MAX                           |
|                                                     |                                    |
--------------------------------------------------------------------------------------------
|                                                     |                                    |
| Relational Operators                                | <, <=, =, =>, >, <>, #             |
|                                                     |                                    |
--------------------------------------------------------------------------------------------
|                                                     |                                    |
| Boolean AND Operators                               | LAND, AND                          |
|                                                     |                                    |
--------------------------------------------------------------------------------------------
|                                                     |                                    |
| Boolean OR Operators                                | LOR, OR, XOR                       |
|                                                     |                                    |
--------------------------------------------------------------------------------------------

Table 3-27 Note 

*   A unary operator is applied to the exponent before the exponentiation
    operator is applied to its arguments.  For example, -2**-2 is
    equivalent to -(2**(-2)).

Examples 

     4+7*2 = 4+(7*2) = 4+14 = 18
     (4+7)*2 = 11*2 = 22
     3-2+1 = (3-2)+1 = 1+1 = 2
     3-(2+1) = 3-3 = 0
     NOT A**3 MOD 12 + 75 = B AND C OR D =
        (((((NOT(A**3)) MOD 12) + 75) = B) AND C) OR D

Result Type 

If an arithmetic operation has two operands of the same type, the
operation is performed using that type.  The intermediate result is of
that type, and an error occurs if the intermediate result is out of the
range of the final result type.  The following are exceptions:

 *  Short integer arithmetic, performed in integer arithmetic.

 *  Exponentiation in which the base is converted to a real for all
    types.  The exponent is converted to a real for decimal, short
    decimal, and short real.  The exponents for integers and short
    integers are not converted.  That is, a short integer remains a short
    integer, and an integer remains an integer.

Examples 

     10 INTEGER A,B
     20 REAL C
     30 C=A+B
     99 END

In line 30 of the above program, the intermediate result of A+B is an
integer.  It is converted to a real number when it is assigned to the
real variable, C.

If an arithmetic operation has two operands of different types, one or
both operands are converted to one type before the operation.  The type
that they are converted to depends on the default numeric type.

Precision can be lost when numbers are converted between real and decimal
types.  Overflow can occur when numbers are converted to a type with a
smaller range (for example, real to short real).



MPE/iX 5.0 Documentation