Expressions, Operators, and
Functions
Manipulating Data
BASIC09 uses expressions to manipulate data. (Expressions are
pieces of data connected by operators.)
An operator is a symbol or a word that signifies some action to
be performed on the specified data. Each data item is a value.
Expressions
When an expression is evaluated, the result is a value of some
data type (real, integer, string, byte, or Boolean).
An expression might look like this:
First First SecondSecond
Value Operator Value Operator Result
6 + 5 = 11
or like this:
First First SecondSecond
Value Operator Value Operator Result
"Seaside" + "Villa" = Seaside
Villa
When BASIC09 evaluates an expression, it copies each value onto
an
expression
stack. Functions and operators take their input
values from this stack and return their results to it. Many
expressions result in assignments, as do the examples shown.
The BASIC09 makes the resulting assignment only after it computes the entire expression. This lets you use the variable that is
being modified as one of the values in the expression, such as in
this example:
X=X+1
7-1
BASIC09
Reference
The result of an expression is always one of the five BASIC09
data types. However, you can often mix data types within an
expression and, in some cases, the result of an expression is of a
different data type than any of the values in the expression.
Such is the case if you use the
less-than
symbol (<), in this
manner:
24 < 1 0 0
The less-than operator compares two integer values. The result
of the comparison is Boolean; in this case, the value is TRUE.
Type Conversion
Because BASIC09 performs automatic type conversion of values,
you can mix any of the three numeric data types in an expression. When you mix numeric data types, the result is always of
the same type as the value having the largest representation, in
this order: real < integer < byte.
You can use any numeric type in an expression that produces a
real number. If you want an expression to produce a byte or integer type value, the result must be small enough to fit the
desired type.
Operators
BASIC09 has operators to deal with all types of data. Each operator, except NOT and negation (unary -), takes two values or
operands,
and performs an operation to produce a result. NOT
can accept only one value. The following table lists the operators
available and the types of data they accept and produce.
Because the same operators function on the three types of
numeric data (byte, integer, and real), these types are referred to
by the operand type "numeric."
7-2
Expressions, Operators, and Functions / 7
BASIC09 Expression Operators
Operand Result
Operator Function Type Type
- Negation numeric numeric
A
or ** Exponentiation numeric numeric
* Multiplication numeric numeric
/ Division numeric numeric
+ Addition numeric numeric
- Subtraction numeric numeric
NOT Logical Negation Boolean Boolean
AND Logical AND Boolean Boolean
OR Logical OR Boolean Boolean
XOR Logical Exclusive OR Boolean Boolean
+ Concatenation string string
= Equal to all types Boolean
< > or > < Not equal to all types Boolean
< Less than numeric, string's Boolean
< = or = < Less than or equal numeric, string
t
Boolean
> Greater than numeric, string
t
Boolean
> = or = > Greater than or equal numeric, string
t
Boolean
i When comparing strings, 1RASIC09 uses the ASCII values of
characters as the basis for comparison. Therefore, 0 < 1, 9 < A,
A<B,A<b,b<z,andsoon.
Arithmetic Operators
Arithmetic operators
perform operations on numeric data. Therefore, both operands in the expression -must be numeric. The following table lists the arithmetic operators.
Negation The single dash negates a number's sign:
-10 is negative 10.
Exponentiation Use a caret C) or two asterisks (**) to raise
a number to a power:
2^3 is 8 (2 x 2 x 2).
Similarly,
2**3 is 8.
Multiplication A single asterisk causes multiplication:
2*3is6.
Division A slash causes division:
6 / 2 is 3.
7-3
BASIC09 Reference
Addition The plus sign causes addition: 3 + 3 is 6.
Subtraction A dash causes subtraction: 6 - 3 is 2.
Hierarchy of Operators
BASIC09 uses the standard hierarchy of operations when calculating expressions with multiple operators. This means that
BASIC09 has an order in which it performs calculations involving more than one operator.
The following BASIC09 operators are listed in order of
precedence:
NOT - (negate)
AND
OR XOR
Also, BASIC09:
· Performs operations enclosed in parentheses before operations not in parentheses.
· Performs the leftmost operations first when two or more
operations are of equal precedence.
You can use parentheses to override this standard precedence.
Fbr example:
2 + 1 * 3 =
but
C 2 + 1 ) * 3 = 9
The following examples show BASIC09 expressions on the left,
and the way BASIC09 evaluates them on the right. You can
enter the expressions in either form, but the decompiler generates the simpler form, shown on the left.
7-4
Expressions, Operators, and Functions / 7
BASIC 09
Representation
Equivalent
Form
a=b+c**2/d
a = b>c AND d>e OR
c=e
a=(b+c+d)/e
a = b**c**d/e
a = -(b)**2
a=b=c
a = b + ((c**2)/d)
a = ((b>c) AND (d>e))
OR (c = e)
a = ((b + c) + d)/e
a = (b**(c**d))/e
a = (-b)**2
Relational Operators
Relational operators make logical comparisons of any type of data
and return a result of either TRUE or FALSE. An explanation of
the relational operators follows. All relational operators have
equal precedence.
Equal. Returns TRUE if both operands are
equal, or FALSE if they are not equal.
Less than: Returns TRUE if the first operand is
less than the second, or FALSE if is not.
Greater than: Returns TRUE if the first operand
is greater than the second, or FALSE if it is not.
< > or > < Unequal: Returns TRUE if the operands are not
equal or FALSE if they are.
< = or = < Less than or equal to: Returns TRUE if the first
operand is less than or equal to the second
operand. Otherwise, the operation returns
FALSE.
> = or
Greater than or equal to: Returns TRUE if the
first operand is greater than or equal to the
second. Otherwise, the operation returns FALSE.
7-5
BASIC09 Reference
You normally use relational operators in IF/THEN statements.
For example, if your procedure has two numeric variables, Payments and Income, you might include command lines like this:
IF PAYMENTS > INCOME THEN
PRINT "You're Broke!"
ENDIF
When you combine arithmetic and relational operators in the
same expression, BASIC09 evaluates the arithmetic operations
first. For example:
IF X*Y/2 <= 14 THEN
PRINT "Average Score
i5
"; X*Y/2
ENDIF
BASIC09 performs the arithmetic operation x*y/2, then compares
the result with the value 14.
When you use relational operators with strings, BASIC09 compares the strings character by character. When it finds two characters that do not match, it checks to see which character has
the lower ASCII code value. The string containing the character
with the lower value comes first.
Consider this example:
PRINT "hunt" > "hung"
BASIC09 compares each character in each string. Because the
first three characters are the same, the result of the operation is
based on the comparison of t and g. Because t (ASCII value
116) is "greater than" g (ASCII value = 103), the command
prints TRUE.
String Operators
The
string operator is
the plus sign (+ ). This symbol appends
one string to another. All operands must be strings, and the
resulting value is one string. Examine, for example, the following line, which appends three strings:
PRINT My friends are + Jack and + Jill.
It prints:
My friends are Jack and Jill.
7-6
Expressions, Operators, and Functions / 7
Logical Operators
The logical, or Boolean, operators make logical comparisons of
Boolean values. The following table describes the results yielded
by each logical operator given the specified TRUE/FALSE values:
Meaning of First Second
Operator Operation Operand Operand Result
NOT The result is the opposite of TRUE FALSE
the operand. FALSE TRUE
AND When both values are TRUE, TRUE TRUE TRUE
the result is TRUE. TRUE FALSE FALSE
Otherwise, the result is FALSE TRUE FALSE
FALSE. FALSE FALSE FALSE
OR when both values are TRUE TRUE TRUE
FALSE, the result is FALSE. TRUE FALSE TRUE
Otherwise, the result is FALSE TRUE TRUE
TRUE. FALSE FALSE FALSE
XOR when only one of the values TRUE TRUE FALSE
is TRUE, the result is TRUE FALSE TRUE
TRUE. Otherwise the result FALSE TRUE TRUE
is FALSE. FALSE FALSE FALSE
Use logical operators in IF/THEN statements such as:
IF PAYMENTS < INCOME AND INCOME+SAVINGS >
PAYMENTS THEN
PRINT "You'll have to use your savings to get
out of this mess."
Functions
Functions are operation sequences the system performs on data.
In a statement, BASIC09 performs functions first. Chapter 11,
"Command Reference," describes the following functions.
7-7
BASIC09 Reference
Functions returning results of type real
SIN
COs
TAN
ASN
ACS
ATN
LOG
EXP
Calculates the trigonometric sine of a number.
Calculates the trigonometric cosine of a number.
Calculates the trigonometric tangent of a number.
Calculates the trigonometric arcsine of a number.
Calculates the trigonometric arccosine of a number.
Calculates the trigonometric arctangent of a
number.
Calculates the natural logarithm (base e) of a
number.
LOG10 Calculates the logarithm (base 10) of a number.
Calculates a (2.71828183) raised to the specified
positive power.
FLOAT Converts byte or integer type numbers to real
numbers.
Calculates the largest whole number less than or
equal to the specified number.
PI Represents the constant 3.14159265.
Calculates the square root of a positive number.
Calculates the square root of a positive number. Its
function is identical to SQR.
Returns a random number.
INT
SQR
SQRT
RND
7-8
Expressions, Operators, and Functions l 7
Functions returning results of any numeric type
The resulting type depends on the input type.
ABS Calculates the absolute value of a number.
SGN Returns a value to indicate the sign of the specified
number (-1 if the number is less than 0, 0 if the
number is 0, or 1 if the number is greater than 0).
SQ Calculates the square of a number.
VAL Converts a string to a numeric value.
Functions returning results of type integer or type byte
FIX Rounds a real number and converts it to an
integer.
MOD Calculates the modulus (remainder) of two
numbers.
ADDR Returns the absolute memory address of a
variable, an array, or a structure.
SIZE Returns (in bytes) the storage size of a variable,
an array, or a structure.
ERR Returns the error code of the most recent error.
PEEK Returns the byte value at a specified memory
address.
POS Returns the current character position of the
print buffer.
ASC Returns the numeric value (ASCII code) of a
string character.
LEN Returns the length of a string. _
SUBSTR Returns the starting position of the specified
substring within a string, or returns 0 if it
cannot find the substring.
7-9
BASIC09 Reference
Functions performing bit-by-bit logical operations on integer or byte data and returning integer results. Do not confuse these functions with Boolean type operators.
LAND Calculates the logical AND of two values.
LOR Calculates the logical OR of two values.
LXOR Calculates the logical EXCLUSIVE OR of two
values.
LNOT Calculates the logical NOT of a value.
Functions returning a result of type string
CHR$ Returns the character having a specified ASCII
value.
DATE$ Returns the system's current date and time.
LEFT$ Returns the specified number of characters
beginning at the leftmost character of the
specified string.
RIGHT$ Returns the specified number of characters
beginning at the rightmost character of the
specified string and counting backward.
MID$ Returns the specified number of characters
starting at the specified position in a string.
STR$ Converts numeric type data to string type.
TRIM$ Removes trailing spaces from the specified
string.
Functions returning Boolean values
TRUE Always returns TRUE.
FALSE Always returns FALSE.
EOF Tests for the end of a disk file. Returns TRUE
when the end of the file occurs.
7-10