Operators(3.1)
The following operators are valid in the APL language.
Operator | Description | Example |
---|---|---|
Arithmetic operators: | ||
+ | Addition (numeric types) or string concatenation (if left operand is of string type). | string1 = string2 + string3; NumFld = NumFld + 1; |
Arithmetic operators (only numeric types): | ||
- | Subtraction | NumFld = NumFld - 2; |
* | Multiplication | NumFld = NumFld * 3; |
/ | Division | NumFld = NumFld / 4; |
% | Modulus | NumFld = NumFld % 10; |
Unary operators (only primitive integer types, cannot be a UDR field): | ||
++ | Increment | NumFld++; //Increment after evaluation ++NumFld; //Increment before evaluation |
-- | Decrement | Â NumFld--; //Decrement after evaluation --NumFld; //Decrement before evaluation; |
Bit operators (only integer types): | ||
& | Bitwise AND | NumFld = NumFld & 1; |
| | Bitwise OR | NumFld = NumFld | 2; |
<< | Shift bits left | NumFld = NumFld << 1; |
>> | Shift bits right | NumFld = NumFld >> 1; |
Boolean operators: | ||
== | Equal to | if (Fld1 == 1) |
!= | Not Equal to | if (Fld1 != 4) |
&& | Logical AND | if (Fld1 == 1 && Fld2 != 4) |
|| | Logical OR | if (Fld1 == 1 || Fld2 != 4) |
<= | Less than or equal to | if (Fld1 <= 5) |
< | Less than | if (Fld1 < 5) |
>= | Greater than or equal to | if (Fld1 >= 5) |
> | Greater than | if (Fld1 > 5) |
! | Not | if (! BoolFld) |
Type conversions for the arithmetic operators follow the Java standard.
The && and || operators have the same precedence. Both operators have right fixity. For clarity and to avoid errors, it is generally recommended to override the precedence rules by using parentheses in expressions.
Example - Operator precedence
consume { boolean a = false; boolean b = false; boolean c = true; boolean x; // The following statement will be parsed as // false && (false || true) // and evaluate to false x = a && b || c; debug(x); // This will evaluate to true x = (a && b) || c; debug(x); }
Java and APL may differ
In some circumstances, Â APL and Java are handling numeric values differently. Â APL may be automatically expanded to avoid overflow, this applies also to constant expressions.
Example 1: A statement like: long x=1000000000000; is ok in APL, but will not compile in Java.
Example 2: An expression like: long x=1000000*1000000; works as expected in APL. But in Java, it would assign x to the value -727379968, due to overflow.