The next change to your sample procedure is to perform a simple calculation and display a value based on the result. This section provides an introduction to representing arithmetic expressions in ABL. It also discusses how to use some of the special built-in functions for advanced arithmetic operations.

Arithmetic expressions and operands

ABL supports the set of arithmetic operands described in the following table. You can use these operands to define expressions. You might be familiar with them from other programming languages you have used.

Table 1. Supported arithmetic operands
Symbol Description
+ Adds numeric values; concatenates character strings
- Subtracts numeric values or date values
* Multiplies numeric values
/ Divides numeric values

The AVM evaluates operators of equal precedence from left to right. Otherwise, the AVM evaluates the operator of higher precedence, as shown in the following table. Use parentheses to raise the precedence of a given expression.

Table 2. ABL operator precedence
Precedence (highest to lowest) Operator function Symbol
11 Numeric negative (unary)

Numeric positive (unary)

-

+

10 Numeric modulo

Numeric division

Numeric multiplication

MODULO

/

*

9 Date subtraction

Datetime subtraction

Numeric subtraction

Date addition

Datetime addition

Numeric addition

String concatenation

-

-

-

+

+

+

+

8 Relational string match

Relational less than

Relational less than or equal to

Relational greater than

Relational greater than or equal to

Relational equal to

Relational not equal to

Relational string beginning

MATCHES

LT or <

LE or <=

GT or >

GE or >=

EQ or =

NE or <>

BEGINS

7 Bitwise NOT (unary) NOT
6 Logical NOT (unary) NOT
5 Bitwise AND AND
4 Bitwise XOR XOR
3 Bitwise OR OR
2 Logical AND AND
1 Logical inclusive OR OR

(The bitwise operators are used with flag enumeration types. For more information, see ABL Reference.)

There is one special thing you need to know when you are writing expressions involving these operands. Because ABL allows the use of a hyphen as a character in a procedure name, variable name, or database field name, the AVM cannot recognize the difference between a hyphen and a minus sign used for subtraction, which are the same keyboard character. For example, there is no way for the syntax analyzer to tell whether the string ABC-DEF represents a single hyphenated variable or field name, or whether it represents the arithmetic expression ABC minus DEF, involving two fields or variables named ABC and DEF. For this reason, you have to put a space or other white space characters around the "-" character when you use it as a minus sign for subtraction of one number from another. Note that you do not have to insert a space after a minus sign that precedes a negative number, such as –25. For consistency, the other arithmetic operands also require white space. If you forget to put it in, you get an error, except in the case of the forward slash character. In the case of the slash, if you leave out the white space, the AVM interprets the value as a date! So, for example, 5/6 represents May 6th, not a numeric fraction.

To illustrate how to use arithmetic operands in the sample procedure, you need to determine whether the CreditLimit of the Customer is less than twice the outstanding Balance. If this is TRUE, then you must display the ratio of CreditLimit to Balance. Otherwise you display the Orders for the Customer. Add the following code, just in front of the FOR EACH Order OF Customer NO-LOCK statement that is already there:

IF Customer.CreditLimit < 2 * Customer.Balance THEN
  DISPLAY "Credit Ratio:" Customer.CreditLimit / Customer.Balance.
ELSE 
  FOR EACH Order OF Customer NO-LOCK:

You can add parentheses to such an expression to make the grouping of terms explicit. Otherwise, the AVM observes the standard rules of precedence. Multiplication and division are performed before addition and subtraction, and all such calculations are performed before a comparison operation.

The expression following the IF keyword compares the CreditLimit field from the current Customer record with two times the value of the Balance field. If the first value is less than the second, then the expression is TRUE and the statement following the THEN keyword is executed, which displays the string expression Credit Ratio: followed by the value of the CreditLimit divided by the Balance.

The ELSE keyword is followed by the entire FOR EACH Order block, so that block of code, which displays all the Orders of the Customer, is skipped if the expression is TRUE, and executed only if it is FALSE.

To see the result of this change, run your procedure again. For a Customer where the ratio is greater than or equal to 2, the Orders display as before. For a Customer where the ratio is less than 2, the new expression is displayed instead:

You might notice a couple of things about this display:

  • Because the CreditLimit check is in the block of code where the procedure retrieves and displays Customers, it is displayed in the same frame as the Customer information. You can give names to frames to be more specific about the frame in which to display objects, as well as where in the frame each element is displayed.
  • The AVM understands enough about what is going on here to clear and hide the Order frame if it is not being displayed for the current Customer (because the CreditLimit to Balance ratio is being displayed instead). This is part of the very powerful default behavior of the language.

Arithmetic built-in functions

The following table describes some of the useful built-in functions that extend the basic set of numeric operands.

Table 3. Arithmetic built-in functions
Function Description
ABSOLUTE Returns the absolute value of a numeric value.
EXP Returns the result of raising a number to a power. The number is called the base and the power is called the exponent.
LOG Calculates the logarithm of an expression using a specified base and returns that logarithm as a DECIMAL value.
MAXIMUM Compares two or more values and returns the largest value.
MINIMUM Compares two or more values and returns the smallest.
MODULO Determines the remainder after division.
RANDOM Returns a random INTEGER value between two integers (inclusive).
ROUND Rounds a decimal expression to a specified number of places after the decimal point.
SQRT Returns the square root (as a DECIMAL value) of an expression you specify.
TRUNCATE Truncates a decimal expression to a specified number of decimal places, returning a decimal value.