BASIC Expressions and Operators - fvdhoef/aquarius-plus GitHub Wiki
An expression may be simply a string or numeric constant, or a variable, or it may combine constants and variables with operators to produce a single value.
Operators perform mathematical or logical operations on values. The operators provided by plusBASIC are grouped into three categories:
- Arithmetic
- Relational
- Logical
- String
The arithmetic operators, in order of precedence, are:
^ |
Exponentition |
- |
Negation |
* / MOD
|
Multiplication, Division, and Modulus |
+ -
|
Addition and Subtraction |
To change the order in which the operations are performed, use parentheses- Operations within parentheses are performed first. Inside parentheses, the usual order of operations is maintained.
Here are some sample algebraic expressions and their plusBASIC counterparts:
Algebraic Expression | plusBASIC Expression |
---|---|
X+2*Y |
|
X-Y/Z |
|
X*Y/Z |
|
(X+Y)/Z |
|
(X^2)^Y |
|
X^(Y^Z) |
|
X*-Y |
A Division by zero error
occurs if:
- during the evaluation of an expression, a division by zero is encountered
- the evaluation of an exponentiation results in zero being raised to a negative power
An Overflow error
results if the absolute value of an expression evaluates to a number greater than
Relational operators are used to compare two values. The result of the comparison is either true -1
or false 0
. This result may then used to make a decision regarding program flow (See IF).
Operator | Symbol | Relation Tested |
---|---|---|
= |
Equality | |
<> |
Inequality | |
< |
Less than | |
> |
Greater than | |
<= |
Less than or equal to | |
>= |
Greater than or equal to |
The equal sign is also used to assign a value to a variable (See LET).
When arithmetic and relational operators are combined in one expression, the arithmetic is always performed first.
For example, the expression X+Y < (T-l)/Z
is true if the value of X
plus Y
is less than the value of T-1
divided by Z
.
More examples:
IF SIN(X) < 0 GOTO l000
IF I MOD J <> 0 THEN K=K+1
Logical operators perform tests on multiple relations, bit manipulation, or Boolean operations. The logical operator returns a bitwise result which is either true (not zero) or false (zero). In an expression, logical operations are performed after arithmetic and relational operations.
The outcome of a logical operation is determined as shown in the following tableS. The operators are listed in order of precedence.
NOT
X | NOT X |
---|---|
1 | 0 |
0 | 1 |
AND
X | Y | X AND Y |
---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
OR
X | Y | X OR Y |
---|---|---|
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
XOR
X | Y | X XOR Y |
---|---|---|
1 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Just as the relational operators can be used to make decisions regarding program flow, logical operators can connect two or more relations and return a true or false value to be used in a decision (see IF).
For example:
IF D<200 AND F<4 THEN 80
IF I>10 OR K<0 THEN 50
IF NOT P THEN 100
Logical operators work by converting their operands to sixteen bit, signed, two's complement integers in the range -65536 to +65535. (If the operands are not in this range, an error results.) If both operands are supplied as or -1, logical operators return or -1,
The given operation is performed on these integers in bitwise fashion. That is, each bit of the result is determined by the corresponding bits in the two operands.
Thus, it is possible to use logical operators to test bytes for a particular bit pattern. For instance, the AND operator may be used to "mask" all but one of the bits of a status byte at a machine I/O port, while the OR operator may be used to merge two bytes to create a particular binary value.
The following examples will help demonstrate how the logical operators work.
Expression | Result | Explanation |
---|---|---|
63 AND 16 |
63 = binary 111111 and 16 = binary 10000, so 63 AND 16 = 16 | |
I5 AND 14 |
15 = binary 1111 and 14 = binary 1110, so 15 AND 14 = 14 (binary 1110) | |
-1 AND 8 |
-1 = binary 1111111111111111 and 8 = binary 1000, so -1 AND 8=8 | |
4 OR 2 |
4 = binary 100 and 2 = binary 10, so 4 OR 2 = 6 (binary 110) | |
10 OR 10 |
10 = binary 1010, so 1010 OR 1010 = 1010 (10) | |
-1 OR -2 |
-1 = binary llllllllllllllll and -2 = binary 1111111111111110, so -1 OR -2 = -1. The bit complement of sixteen zeros is sixteen ones, which is the two's complement representation of -1) | |
NOT X |
The two's complement of any integer is the bit complement plus one |
The concatenation operator +
joins to strings together to create a new string.
For example:
10 AS$="FILE":B$="NAME"
20 PRINT A$+B$
30 PRINT "NEW "+A$+B$
RUN
FILENAME
NEW FILENAME
Strings may be compared using the same relational operators that are used with numbers. String comparisons are made by taking one character at a time from each string and comparing the ASCII codes.
- If all the ASCII codes are the same, the strings are equal.
- If the ASCII codes differ, the lower code number precedes the higher.
- If, during string comparison, the end of one string is reached, the shorter string is said to be smaller.
- Leading and trailing blanks are significant.
Examples:
"AA"
< "AB"
"FILENAME"
= "FILENAME"
"X&"
> "X#"
"CL "
> "CL"
"kg"
> "KG"
"SMYTH"
< "SMYTHE"`
B$
< "9/12/78"
where B$
= "8/12/78"
Thus, string comparisons can be used to test string values or to alphabetize strings. All string constants used in comparison expressions must be enclosed in quotation marks.
The string substitution operator %
embeds values in a target string.
The syntax is string_expression %
(
expression_list )
where each occurence of the text '%%' in the list is substituted with the value of the matching expression in the list.
- expression_list may contain both numeric and string operations.
-
Syntax error
results if the number of%%
specifiers in string_expression is not equal to the number of expressions in expression_list.
Examples:
A=1:B=2
PRINT "%% + %% = %%" % (A, B, A+B)
1 + 2 = 3
F$="Fred":L$="Jones"
N$="%%, %%" % (L$,F$)
PRINT N$
Jones, Fred
F$="COS":A=45
X$="%%(%%)" % (F$,A)
PRINT "%% = %%" % (X$,EVAL(X$))
COS(45) = .525324