Expressions and Operators

From NSB App Studio
Jump to: navigation, search

An expression is a literal, variable, formula or FUNCTION procedure call that has a value. Here are some examples of expressions:
6/3 'result is 2
5+6/3 'result is 7
"This " & "that" 'result is "This that"

A string expression can be a string literal, a string variable, or it may combine string literals, string variables and substrings to produce a single string value. Similarly, a numeric expression can be a numeric constant, a numeric variable, or a function/variable that produces a single numeric value.


2.3.1 Arithmetic Operators

AppStudio allows the following arithmetic operators in this descending order of priority:

( ) Parenthesis
^ Exponentiation
* / \ Multiplication and Division
+ - Addition and Subtraction
 % Remainder/Modulus

Parenthesis can be used to change the order of evaluation.
PRINT 2 + 3 * 4
PRINT (2 + 3) * 4

Displays
14
20

AppStudio supports floating-point arithmetic. The MOD function may be used to find the remainder of a division. The backslash operator (\) is used for integer (whole number) division.
Arithmetic operators can only be used with numeric expressions. They may not be used with strings.


2.3.2 Relational Operators

Relational operators compare two values and return a Boolean value of TRUE or FALSE. This result can be used to change the flow of a program. Relational operators have a lower priority than arithmetic operators. The relational are:

= Equal
<> Not Equal
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to

In the SET statement, the equal sign is used to assign a value to a variable, not as a relational operator.


2.3.3 Boolean Operators

Boolean operators tie expressions together, returning a TRUE or FALSE answer. Arithmetic and relational operators are evaluated before Boolean operators. The NOT operator applies to one expression, while the other operators require two expressions.

The Boolean operators are:

AND Returns TRUE if the two expressions are both TRUE.
EQV Returns TRUE if both expressions are TRUE or both expressions are FALSE.
IMP Returns TRUE if the first expression implies the second expression.
OR Returns TRUE if either expression or both of the expressions are TRUE.
NOT Returns TRUE if the expression is FALSE or returns FALSE if it is TRUE.
XOR Returns TRUE if the neither of the expressions matches the other.

Boolean operators can be used with any expression that returns a Boolean value.


2.3.4 Bit-wise Operators

Since all numbers in AppStudio are floating point, and bit-wise operators only work on integers, AppStudio does a little behind-the-scenes magic to make it appear that bit-wise operations are being applied to a 32-bit signed integer.

Specifically, it takes the number you are working on and takes the integer portion of the number. It then converts the integer to the most number of bits that number represents, up to 31 bits (1 bit for the sign). So 0 would create a two bit number (1 for the sign, and 1 bit for 0), likewise 1 would create two bits. 2 would create a 3 bit number, 4 would create a 4 bit number, etc…

It's important to realize that you're not guaranteed a 32 bit number. For instance applying "not" to zero should, in theory, convert 0 to 4,294,967,295. Instead it will return -1 for two reasons; the first being that all numbers are signed so not always reverses the sign, and second AppStudio couldn't make more than one bit from the number zero. Therefore "not" zero becomes one (~0=-1). Consider the following summary:

Operator Description Notes
ANDb AND 1&1=1,1&0=0
ORb OR 0=1, 0|0=0
XORb XOR 1^1=0, 1^0=1
NOTb NOT (Unary) ~1=0, ~0=-1
<< Shift Left 1<<2=4 -- shift 1, 2 bits left
>> Shift Right 4>>2=1 -- shift 4, 2 bits right (*)
>>> Shift Right 4>>>2=1 -- shift 4, 2 bits right (*)

(*) When shifting right, double greater-thans (>>) will fill the new bits to the far left as 1s if the number was negative and zero if the number was positive. Triple greater-thans (>>>) is the same as double greater-thans but the fill bit will always be zero so the sign of the original number is not preserved and the result is always positive.

Next: Function and Sub Procedures