Expressions
Numeric Expression <nexp>
A numeric expression consists of one or more numeric variables or numeric constants separated by binary operators and optionally preceded by unary operators. The definition can be stated more completely using this standard formal notation:
<nexp> := {<numeric variable>|<numeric constant} {<noperator> <nexp>}
The next few sections define all of the terms.
Numeric Operators <noperator>
The numeric operators are listed by precedence. Higher precedence operators are executed before lower precedence operators. Precedence can be changed by using parentheses.
- Unary
+
, Unary–
- Exponent
^
- Multiply
*
, Divide/
- Add
+
, Subtract–
Note that the comma (',') is not an operator in BASIC!. It is sometimes uses as a separator between expressions; for example, see the PRINT
command.
Numeric Expression Examples
a
a*b + 4/d – 2*(d^2)
a + b + d + RND()
b + CEIL(d/25) + 5
Pre- and Post-Increment Operators
++x
Increments the value of x by 1 before the x value is used
--y
Decrements the value of y by 1 before the y value is used
x++
Increments the value of x by 1 after the x value is used
y--
Decrements the value of y by 1 after the y value is used
a = 5 % creates the variable a and sets it to 5
PRINT --a % sets a to 4 and prints 4
PRINT a-- % prints 4 and sets a to 3
These operations work only on numeric variables. Their action is performed as part of evaluating the variable, so they do not follow normal precedence rules.
Using these operators on a variable makes the variable unavailable for other operations that require a variable. For example, you cannot pass a variable by reference (see User-Defined functions
) if you pre- or post-increment or -decrement it, because you cannot pass an expression by reference. An exception is made to allow implicit assignment (actual or implied LET
).
String Expression <sexp>
A string expression consists of one or more string variables or string constants separated by '+' operators. The definition can be stated more completely using this standard formal notation:
<sexp> := {<string variable>|<string constant>} { + <sexp>}
There is only one string operator: +. This is the concatenation operator. It is used to join two strings:
PRINT "abc" + "def" % prints abcdef
Logical Expression <lexp>
Logical expressions, or Boolean expressions, produce only two results: false or true. False is represented in BASIC! by the numeric value of zero. Anything that is not zero is true. False = 0. True = not 0.
There are two types of logical expressions: Numeric logical expressions and string logical expressions. Both types produce a numerically-represented values of true or false. Each type consists of one or more variables or constants separated by binary logical operators, formally defined like this:
<slexp> := {<string variable>|<string constant>} <logical operator> {<string variable>|<string constant>}
<nlexp> := {<numeric variable>|<numeric constant>} <logical operator> {<numeric variable>|<numeric constant>}
There is also the unique unary NOT (!) operator. NOT inverts the truth of a logical expression.
Logical Operators
Most of the logical operators are used for comparison. You can compare strings or numbers (<, =, etc.). You can use the other Boolean operators (!, &, |) on numbers but not on strings.
This table shows all of the Logical operators. They are listed by precedence with the highest precedence first. Precedence may be modified by using parentheses.
Precedence | Operator | Meaning | Operands |
---|---|---|---|
1 | ! |
Unary Not | One <nlexp> only |
2 | < > <= >= |
Less Than, Greater Than, Less Than or Equal, Greater Than or Equal |
Two <nlexp> or two <slexp> |
3 | = <> |
Equal, Not Equal |
Two <nlexp> or two <slexp> |
4 | & | |
And, Or |
Two <nlexp> only |
Examples of Logical Expressions
1 < 2 (true)
3 <> 4 (true)
"a" < "bcd" (true)
1 & 0 (false)
!(1 & 0) (true)
Parentheses
Parentheses can be used to override operator precendence.
a = b * c + d % the multiplication is done first
a = b * (c + d) % the addition is done first
Parentheses can also be placed around a variable, anywhere except to the left of an = sign. This can be useful in places where BASIC! may mistake part of a variable for a special keyword. For an example, see Program Control Commands – For - To - Step / Next
, below.