Operators

An operator is a representation of an operation, such as addition, to be carried out on one or two terms. This topic describes how each operator (except for the prefix operators) acts on two terms, which can be symbols, strings, function calls, intermediate results, or subexpressions. Each prefix operator acts on the term or subexpression that follows it.

Blanks (and comments) adjacent to operator characters do not affect the operator; thus, operators constructed from more than one character can have embedded blanks and comments. In addition, one or more blanks, where they occur in expressions but are not adjacent to another operator, also act as an operator.

There are four types of operators:
  • Concatenation
  • Arithmetic
  • Comparison
  • Logical

String concatenation operators

The concatenation operators combine two strings to form one string by appending the second string to the right-hand end of the first string. The concatenation can occur with or without an intervening blank.

You can force concatenation without a blank by using the || operator.

The abuttal operator is assumed between two terms that are not separated by another operator. This can occur when two terms are syntactically distinct, such as a literal string and a symbol, or when they are separated only by a comment.

Table 1. Concatenation operators
Operator Description
(blank) Concatenate terms with one blank in between
|| Concatenate without an intervening blank
(abuttal) Concatenate without an intervening blank
Examples:

An example of syntactically distinct terms is: if Fred has the value 37.4, Fred'%' evaluates to 37.4%.

If the variable PETER has the value 1, (Fred)(Peter) evaluates to 37.41.

In EBCDIC, the two adjoining strings, one hexadecimal and one literal, 'c1 c2'x'CDE' evaluate to ABCDE.

In the following example, there is no abuttal operator implied, and the expression is not valid.

Fred/* The NOT operator precedes Peter. */¬Peter

However, the following example results in an abuttal, and evaluates to 37.40.

(Fred)/* The NOT operator precedes Peter. */(¬Peter)

Arithmetic operators

You can combine character strings that are valid numbers (see Tokens) by using the arithmetic operators in Table 2.
Table 2. Arithmetic operators
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
% Integer divide (divide and return the integer part of the result)
// Remainder (divide and return the remainder - not modulo, because the result may be negative)
** Power (raise a number to a whole-number power)
Prefix - Same as the subtraction: 0 - number
Prefix + Same as the addition: 0 + number.

See Numbers and arithmetic operations for details about precision, the format of valid numbers, and the operation rules for arithmetic. If an arithmetic result is shown in exponential notation, it is likely that rounding has occurred.

Comparison operators

The comparison operators compare two terms and return the value 1 if the result of the comparison is true, or 0 otherwise.

The strict comparison operators all have one of the characters defining the operator doubled. The ==, \==, /==, and ¬== operators test for an exact match between two strings. The two strings must be identical (character by character) and of the same length to be considered strictly equal. Similarly, the strict comparison operators such as >> or << carry out a simple character-by-character comparison, with no padding of either of the strings being compared. The comparison of the two strings is from left to right. If one string is shorter than and is a leading substring of another, then it is smaller than (less than) the other. The strict comparison operators also do not attempt to perform a numeric comparison on the two operands.

For all the other comparison operators, if both terms involved are numeric, a numeric comparison (in which leading zeros are ignored, see Numeric comparisons) occurs. Otherwise, both terms are treated as character strings (leading and trailing blanks are ignored, and then the shorter string is padded with blanks on the right).

Character comparison and strict comparison operations are both case-sensitive, and for both the exact collating order depends on the character set used for the implementation. For example, in an EBCDIC environment, lowercase alphabetic characters precede uppercase, and the digits 0-9 are higher than all alphabetic characters.

Table 3. Comparison operators and operations
Operator Description
= True if the terms are equal (numerically or when padded, and so forth)
\=, ¬=, /= True if the terms are not equal (inverse of =)
> Greater than
< Less than
>< Greater than or less than (same as not equal)
<> Greater than or less than (same as not equal)
>= Greater than or equal to
\<, ¬< Not less than
<= Less than or equal to
\>, ¬> Not greater than
== True if terms are strictly equal (identical)
\==, ¬==, /== True if the terms are NOT strictly equal (inverse of ==)
>> Strictly greater than
<< Strictly less than
>>= Strictly greater than or equal to
\<<, ¬<< Strictly NOT less than
<<= Strictly less than or equal to
\>>, ¬>> Strictly NOT greater than
Note: Throughout the language, the not character, ¬, is synonymous with the backslash (\). You can use the two characters interchangeably, according to availability and personal preference. The backslash can appear in the following operators: \ (prefix not), \=, \==, \<, \>, \<<, and \>>.

Logical (Boolean) operators

A character string is taken to have the value false if it is 0, and true if it is 1. The logical operators take one or two such values (values other than 0 or 1 are not allowed) and return 0 or 1 as appropriate.

Table 4. Logical (Boolean) operators
Operator Description
& AND. Returns 1 if both terms are true.
| Inclusive OR. Returns 1 if either term is true.
&& Exclusive OR. Returns 1 if either (but not both) is true.
Prefix \,¬ Logical NOT. Negates; 1 becomes 0, and 0 becomes 1.