Expressions and Operators: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
Brendon (talk | contribs)
Created page with "An expression is a literal, variable, formula or FUNCTION procedure call that has a value. Here are some examples of expressions: <br /> <tt>6/3 'result is 2 <br /> 5+6/3 'res..."
 
Brendon (talk | contribs)
No edit summary
Line 28: Line 28:
PRINT (2 + 3) * 4</tt> <br />
PRINT (2 + 3) * 4</tt> <br />


Displays
Displays <br />
<tt>14 <br />
<tt>14 <br />
20</tt>
20</tt>

Revision as of 02:39, 1 August 2012

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


NS Basic/App Studio 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

NS Basic/App Studio 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 Bitwise Operators


Since all numbers in NS Basic/App Studio are floating point, and bitwise operators only work on integers, NS Basic/App Studio does a little behind the scenes magic to make it appear bitwise operations are being applied to a 32bit 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 running not on 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 NS Basic/App Studio couldn't make more than one bit from the number zero and not zero becomes one. Therefore ~0=-1.

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.