Operators [ HP Business BASIC/XL Reference Manual ] MPE/iX 5.0 Documentation
HP Business BASIC/XL Reference Manual
Operators
HP Business BASIC/XL has three unary operators: unary plus(+), unary
minus (-), and NOT. All other HP Business BASIC/XL operators are binary.
Also, each HP Business BASIC/XL operator is either an arithmetic,
relational, Boolean, or string operator, depending on the types of its
operands and result.
For each operator category, Table 3-19 gives the operand and result
types.
Table 3-19. Operands and Result Types of Operators
----------------------------------------------------------------------------------------------
| | | |
| Operator | Operand Type | Result Type |
| Category | | |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| Arithmetic | Numeric | Numeric |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| Relational | Numeric or string | Boolean* |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| Boolean | Boolean* | Boolean* |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| String | String | String |
| | | |
----------------------------------------------------------------------------------------------
Table 3-19 Note
* A Boolean value is actually a numeric value. TRUE is one and FALSE
is zero.
Arithmetic Operators
An arithmetic operator has numeric operands and a numeric result.
Table 3-20 identifies each arithmetic operator as unary or binary and
gives its name and an example.
Table 3-20. Arithmetic Operators
-------------------------------------------------------------------------------------------------
| | | | |
| Operator | Unary | Operation Name | Example |
| | or | | (expression=result) |
| | Binary | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| + | Unary | Unary plus | +5=5 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| + | Binary | Addition | 1+2=3 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| - | Unary | Unary minus | -(4+4)=-8 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| - | Binary | Subtraction | 8-4=4 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| * | Binary | Multiplication | 9*7=63 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| / | Binary | Real division | 36/4=9.0 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| DIV | Binary | Integer division | 37 DIV 4=9 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| MOD | Binary | Modulus | 37 MOD 4=1 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| ^ | Binary | Exponentiation | 2^3=8 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| ** | Binary | Exponentiation | 2**3=8 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| MIN | Binary | Minimum | 5 MIN 4=4 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| MAX | Binary | Maximum | 5 MAX 4=5 |
| | | | |
-------------------------------------------------------------------------------------------------
The result of real division is of the default numeric type. The result
of integer division is truncated to a whole number. If the result is
within range, the type is integer. Otherwise, it is decimal or real.
Examples
The following examples show the results of division on different data
types:
3 DIV 2 = 1 -10 DIV 5 = -2 9.999999999 DIV 1 = 9
3/2 = 1.5 -10/5 = -2 9.999999999/1 = 9.999999999
The result of the operation
num_expr1 MOD num_expr2
is
num_expr1-(num_expr2*INT(num_expr1/num_expr2))
where INT(x) returns the largest integer less than or equal to x, for any
numeric expression x. By definition, x MOD 0 = x for any numeric
expression x. The result of the MOD operation is of the default numeric
type, DECIMAL or REAL.
Examples
The following are examples of the result of the MOD statement. Each
example shows the math required to determine the result.
38 MOD 6 = 38 - (6*INT(38/6))
= 38 - (6*6)
= 38 - 36
= 2
13 MOD -2 = 13 - (-2*INT(13/-2))
= 13 - (-2*-7)
= 13 - 14
= -1
-13 MOD 2 = -13 - (2*INT(-13/2))
= -13 - (2*-7)
= -13 - (-14)
= -13 + 14
= 1
-13 MOD -2 = -13 - (-2*INT(-13/-2))
= -13 - (-2*6)
= -13 - (-12)
= -13 +12
= -1
3 MOD 5 = 3 - (5*INT(3/5))
= 3 - (5*0)
= 3 - 0
= 3
Relational Operators
A relational operator has either two numeric operands, two ASCII string
operands, or the result of another relational expression and a Boolean
result. Every relational operator is binary.
Table 3-21 gives the name and an example of each relational operator.
Table 3-21. Relational Operators
-----------------------------------------------------------------------------------------------
| | | |
| Operator | Operation Name | Example (expression=result) |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| < | Less Than | (1<2)=TRUE |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| <= | Less Than or Equal | (2<=1)=FALSE |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| = | Equal | (9=7)=FALSE |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| >= | Greater Than or Equal | (9>=4)=TRUE |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| <> | Not Equal | (36<>45)=TRUE |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| # | Not Equal | 12#(6+6)=FALSE |
| | | |
-----------------------------------------------------------------------------------------------
String Comparisons. String comparisons are made by comparing each string
operand character by character from left to right. The characters are
compared based on each character's ordinal value in the ASCII character
set. The ordinal value of a character is the value in the decimal code
column in the ASCII character code table presented in Appendix D.
To compare two strings when using a native language other than
NATIVE-3000(language #0), the language the system uses before the
introduction of Native Language Support, use the LEX function (for more
information on Native Language Support refer to "Native Language Support"
in chapter 6, or the Native Language Programmer's Guide).
The null string ("") is less than every string except itself, to which it
is equal. The following explanation does not apply to the null string.
HP Business BASIC/XL compares the strings S1$ and S2$ as follows
(S1$[c;1] and S2$[c;1] are corresponding characters).
1. c=1
2. If CHR$(S1$[c;1])<CHR$(S2$[c;1]), then S1$ is less than S2$.
Stop.
3. If CHR$(S1$[c;1])>CHR$(S2$[c;1]), then S1$ is greater than S2$.
Stop.
4. CHR$(S1$[c;1])=CHR$(S2$[c;1]). If c+1 is in the range [1, MIN(
LEN(S1$), LEN(S2$) )], then c=c+1 and return to step 2.
5. If LEN(S1$) = LEN(s2$) then S1$ is equal to S2$. Stop.
6. If LEN(S1$) > LEN(s2$) then S1$ is greater than S2$. Stop.
7. If LEN(S1$) < LEN(s2$) then S1$ is less than S2$. Stop.
(MIN and LEN are the predefined minimum and length functions.)
Examples
The following expressions are TRUE:
"Abc" = "Abc" "Cat" <> "Cats" "Bird" < "Cats"
"Abc" <= "Abc" "Cat" < "Cats" "Ears" > "Early"
"Abc" >= "Abc" "Cat" <= "Cats" "Bird " + "Dog" = "Bird Dog"
The following expressions are FALSE:
"Abc" # "Abc" "Cat" = "Cats" "Bird" >= "Cats"
"Abc" < "Abc" "Cat" < "Bats" "Ears" < "Early"
"Abc" > "Abc" "BAT" = "bat" "Bird" + "Dog" = "Bird Dog"
Boolean Operators
A Boolean operator has one or two operands and a Boolean result.
The Boolean values TRUE and FALSE are represented by the numeric values
one and zero. The operands of a Boolean expression can be Boolean or
numeric values. A numeric operand is considered TRUE if it is nonzero
and FALSE if it is zero.
HP Business BASIC/XL also provides the two keywords TRUE and FALSE. TRUE
is a numeric constant of short integer type equal to one. FALSE is a
numeric constant of short integer type equal to zero. Depending on the
operator, HP Business BASIC/XL evaluates a Boolean expression either
completely or partially.
Logical HP Business BASIC/XL always evaluates both operands.
evaluation
Partial HP Business BASIC/XL always evaluates the first operand,
evaluation but evaluates the second operand only if its value could
change the value of the expression.
Table 3-22 identifies each Boolean operator as unary or binary, gives its
name, and tells whether it is evaluated logically or partially.
Table 3-22. Boolean Operators
----------------------------------------------------------------------------------------------------
| | | | |
| Operator | Unary or Binary | Operation Name | Logical or Partial |
| | | | Evaluation |
| | | | |
----------------------------------------------------------------------------------------------------
| | | | |
| NOT | Unary | Negation | Logical |
| | | | |
----------------------------------------------------------------------------------------------------
| | | | |
| LAND | Binary | Logical AND | Logical |
| | | | |
----------------------------------------------------------------------------------------------------
| | | | |
| AND | Binary | AND | Partial |
| | | | |
----------------------------------------------------------------------------------------------------
| | | | |
| LOR | Binary | Logical OR | Logical |
| | | | |
----------------------------------------------------------------------------------------------------
| | | | |
| OR | Binary | OR | Partial |
| | | | |
----------------------------------------------------------------------------------------------------
| | | | |
| XOR | Binary | Exclusive OR | Logical |
| | | | |
----------------------------------------------------------------------------------------------------
Table 3-23 is the truth table for the NOT operator.
Table 3-23. NOT Truth Table
---------------------------------------------------------------------------------------------
| | |
| X | NOT X |
| | |
---------------------------------------------------------------------------------------------
| | |
| TRUE | FALSE |
| | |
---------------------------------------------------------------------------------------------
| | |
| FALSE | TRUE |
| | |
---------------------------------------------------------------------------------------------
Examples
These expressions are TRUE:
NOT 0 NOT (X-X) NOT (5 = 3) NOT ("HP" < "Competitors)
These expressions are FALSE:
NOT 1 NOT 3600 NOT (5 > 3) NOT("HP" # "Hewlett Packard")
Table 3-24 is the truth table for the LAND and AND operators. The AND
operator evaluates the first operand, and if it is FALSE the result is
FALSE and the second operand is not evaluated. The LAND operator
evaluates both operands regardless of the value of the first one.
Table 3-24. LAND/AND Truth Table
----------------------------------------------------------------------------------------------
| | | |
| X | Y | X {LAND AND} Y |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| TRUE | TRUE | TRUE |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| TRUE | FALSE | FALSE |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| FALSE | TRUE | FALSE |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| FALSE | FALSE | FALSE |
| | | |
----------------------------------------------------------------------------------------------
Examples
These expressions are TRUE:
(2 > 1) AND (1 > 0) (2 > 1) LAND (1 > 0)
((X-1) <= X) AND (X <= (X+1)) ((X-1) <= X) LAND (X <= (X+1))
1 AND (1+0) 1 LAND (1+0)
(3*5) AND ((1+2)/5) (3*5) LAND ((1+2)/5)
("a" < "b") AND ("ant" < "bug") ("a" < "b") LAND ("ant" < "bug")
These expressions are FALSE:
(2 = 1) AND (1 > 0) (2 = 1) LAND (1 > 0)
((X-1) <= X) AND (X > (X+1)) ((X-1) <= X) LAND (X > (X+1))
(3*5) AND (0/5) (3*5) LAND (0/5)
("a" = "ant") AND ("b" = "bug") ("a" = "ant") LAND ("b" = "bug")
The program on the left below evaluates FNI(I); the program on the right
does not. If the function FNI adds one to its argument, then the program
on the left prints "0 1" and the program on the right prints "0 0".
10 I=0 10 I=0
20 PRINT (I LAND FNI(I)); I 20 PRINT (I AND FNI(I)); I
99 END 99 END
If array A has Maxindex elements, and Index is greater than Maxindex,
then the statement
100 IF (Index <= Maxindex) AND (A(Index) =5) THEN GOTO 500
does not evaluate A(Index), and an error does not occur. The statement
200 IF (Index <= Maxindex) LAND (A(Index) = 5) THEN GOTO 600
does evaluate A(Index), and an error occurs (subscript out of range).
Table 3-25 is the truth table for the LOR and OR operators. The OR
operator evaluates the first operand, and if it is TRUE, the result is
TRUE and the second operand is not evaluated. The LOR operator evaluates
both operands regardless of the value of the first.
Table 3-25. LOR/OR Truth Table
----------------------------------------------------------------------------------------------
| | | |
| X | Y | X {OR LOR} Y |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| TRUE | TRUE | TRUE |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| TRUE | FALSE | TRUE |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| FALSE | TRUE | TRUE |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| FALSE | FALSE | FALSE |
| | | |
----------------------------------------------------------------------------------------------
Examples
These expressions are TRUE:
(X < (X+1)) OR (2 < 3) (X < (X+1)) LOR (2 < 3)
(X <= (X+1)) OR (5 = 3) (X <= (X+1)) LOR (5 = 3)
(9-(3**2)) OR ("a" < "z") (9-(3**2)) LOR ("a" < "z")
These expressions are FALSE:
0 OR (5-5) 0 LOR (5-5)
(9-(3**2)) OR (9-(6+3)) (9-(3**2)) LOR (9-(6+3))
(X-X) OR ("a" > "z") (X-X) LOR ("a" > "z")
The program on the left below evaluates FNI(I); the program on the right
does not. If the function FNI subtracts one from its argument, then the
program on the left prints "1 0" and the program on the right prints "1
1".
10 I=1 10 I=1
20 PRINT (I LOR FNI(I)); I 20 PRINT (I OR FNI(I)); I
99 END 99 END
If array A has Maxindex elements, and Index is greater than Maxindex,
then the statement
100 IF (Index &> Maxindex) OR (A(Index) =5) THEN GOTO 500
does not evaluate A(Index), and an error does not occur. The statement
200 IF (Index &> Maxindex) LOR (A(Index) =5) THEN GOTO 600
does evaluate A(Index), and an error occurs (subscript out of range).
Table 3-26 is the truth table for the XOR operator. XOR is different
from the OR and LOR operators in that it returns TRUE only when the first
or the second operator is TRUE, but the operators are not both TRUE. The
OR and LOR operators return TRUE if one or both operands are TRUE.
Table 3-26. XOR Truth Table
----------------------------------------------------------------------------------------------
| | | |
| X | Y | X OR Y |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| TRUE | TRUE | FALSE |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| TRUE | FALSE | TRUE |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| FALSE | TRUE | TRUE |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| FALSE | FALSE | FALSE |
| | | |
----------------------------------------------------------------------------------------------
Examples These expressions are TRUE:
0 XOR 1 (3-(2+1)) XOR 85 (6 <=5) XOR (7+3)
1 XOR 0 35677 XOR (9-(3*3)) (X < (X-1) XOR ("A" = "A")
These expressions are FALSE:
0 XOR 0 ("cat" = "dog") XOR ("a" = "b") (X = (X+1)) XOR (X-X)
1 XOR 1 ("cat" < "dog") XOR ("a" < "b") 365 XOR 366
String Concatenation Operator
The string concatenation operator has two string operands and a string
result.
Syntax
str_expr1 + str_expr2
The resulting string is the value of str_expr1 with the value of
str_expr2 appended to it. The length of the resulting string is the sum
of the two lengths.
Example
10 Mystery1$="hot"+"dog" !Mystery1$'s length is set to 6
20 Mystery2$="base"+"ball" !Mystery2$'s length is set to 8
30 PRINT Mystery1$+"s"+" and "+Mystery2$
40 ! Line 30 prints -- hotdogs and baseball
MPE/iX 5.0 Documentation