| Previous | Contents | Index |
When relational operations are performed on strings, SheerPower determines which string occurs first in the ASCII collating sequence and returns TRUE or FALSE. For instance, when you perform relational operations on two strings, SheerPower checks the ASCII values for each character in each string. SheerPower compares the strings character by character using these ASCII values, and determines where there is a difference in the values.
When SheerPower finds a character that differs, it compares the two and determines which one has a smaller ASCII code number. SheerPower then returns a TRUE or FALSE value depending on the relational expression. For example:
| Example 4-11 Performing relational operations on strings |
|---|
a$ = 'TEXT' b$ = 'TEST' MESSAGE$ = 'Strings are equal' if a$ < b$ then message$ = a$ + ' is less than ' + b$ if b$ < a$ then message$ = b$ + ' is less than ' + a$ print message$ end TEST is less than TEXT |
SheerPower compares the two strings. They are identical up to the third character. The ASCII value of S is 53. The ASCII value of X is 58. Therefore SheerPower prints "TEST is less than TEXT".
The logical operators are:
NOT NOT X TRUE if X is false and
FALSE if X is true.
AND X AND Y TRUE if X and Y are true.
OR X OR Y TRUE if X or Y is true.
XOR X XOR Y TRUE if X is true, or if Y is true but
FALSE if both X and Y are true.
EQV X EQV Y TRUE if X and Y are true, or
TRUE if X and Y are false,
but FALSE otherwise.
IMP X IMP Y TRUE if X is true and Y is false.
|
X and Y can be any expressions. Logical operators are usually used on integers or expressions which yield an integer result such as conditional expressions. Logical operators will always yield an integer result. If a logical operator is used on a real number, the real number is rounded and the resulting integer is used.
Logical expressions always return an integer value. If the integer value is a 1, the expression is TRUE. If the integer value is a 0, the expression is FALSE. (NOT 0 is equal to -1 and is TRUE. NOT 1 is equal to -2 and is FALSE.)
VALUE TRUE FALSE
+--------------------------+
| 0 | | X |
|-----------|------|-------|
| 1 | X | |
|-----------|------|-------|
| NOT 0 (-1)| X | |
|-----------|------|-------|
| NOT 1 (-2)| | X |
+--------------------------+
|
Logical operators can be used to do bit manipulation. Computers represent values in a binary code, using ones and zeros. SheerPower integer values are represented as a 32-bit binary longword. A bit which is set to 1 is considered on. A bit which is set to 0 is off. The value of the word is equal to the value of all the bits which are on, added together. For example:
0 0 0 1 0 1 1 1 = 16 + 4 + 2 + 1 = 23
|
The last bit has a value of 1. The second to the last bit has a value of 2. The third bit has a value of 4, the fourth a value of 8, the fifth bit has a value of 16, and so on. Each bit has a value double that of the previous one:
0 0 0 0 0 0 0 0
---------------------------------------------
128 64 32 16 8 4 2 1
|
Bits can be manipulated and tested using logical operators. The logical operators work on bits. They compare each position in each word according to the particular rules of the logical operator. For instance, here is the AND operator used on two values:
| Example 4-12 Bit manipulation |
|---|
let a% = 23% // 00010111 let b% = 37% // 00100101 let c% = (a% and b%) print c% end 5 |
When SheerPower executes this program, it compares the two values. It sets a bit in the result to 1 (on) only if both the bits at a given position are on (1). The value of the resultant word is 5:
A% 0 0 0 1 0 1 1 1 = 23
B% 0 0 1 0 0 1 0 1 = 37
---------------
C% 0 0 0 0 0 1 0 1 = 5
|
When SheerPower evaluates an expression, it evaluates it in a specific order. SheerPower evaluates expressions from left to right.
1+Z+4 equals (1+Z)+4
1+Z-4 equals (1+Z)-4
3*4/QUANTITY equals (3*4)/QUANTITY
12/QUANTITY*3 equals (12/QUANTITY)*3
|
The following priorities take precedence over the left to right evaluation rule:
Z%-(X% / (Y% + AMOUNT))
|
NOT
AND
OR
XOR
IMP
EQV
Parentheses are used around every set of operations. This makes it easy to pick out the beginning and end of an operation, and will make absolutely clear what is intended without depending on the order of precedence of operators. If someone reading the code can know with absolute certainty how it was intended to be executed via the use of parentheses, then they will not have to wonder if a particular bug is due to the language used creating the code, other than what was intended. See Appendix A, Coding Principles and Standards for more on Coding Principles and Standards in SheerPower.
if a > b * c or d + 4 = f then x = x + 1 <--- hard to read! if ((a>(b * c)) or ((d + 4) = f)) then x = x + 1 <--- clarity with parenthesis |
Variables specify storage locations. Numeric and string variables are assigned values with the LET statement. String variables can also be assigned values with the LSET, RSET and CSET statements.
The LET statement assigns the value of an expression to a variable. The expression is evaluated and its value is stored in the location specified by the variable. The data types of the expression and the variable must match. Thus, you must assign a string variable string values and you must assign numeric variables numeric values. A string variable must end with a dollar sign "$" unless you declare it. An integer variable must end with a percent sign (%) unless you declare it.
The DECLARE statement allows you to dispense with data type designations. DECLARE declares a variable as either string, integer or real. Once the variable has been declared, you do not need to attach a $ or % to it. Functions, arrays, etc., can also be declared with the DECLARE statement.
DECLARE [STRING | INTEGER | REAL | BOOLEAN | OBJECT] var, var...
|
| Example 5-1 DECLARE statement |
|---|
declare string name, sex declare integer age declare real amount declare object anything declare boolean is_male input 'Enter your name': name input 'Enter your age': age input 'Enter your sex': sex input 'Enter an amount': amount if sex = 'male' then is_male = true else is_male = false print print name; ' is a'; age; 'year old '; sex print name; ' entered the amount'; amount if is_male then print name; ' is male' anything = 4 + 5 print 'This object (or dynamic) variable contains a numeric value: '; anything anything = 'kitty' print 'Now it contains a string value: '; anything end Enter your name? Sammy Enter your age? 28 Enter your sex? male Enter an amount? 25.38 Sammy is a 28 year old male Sammy entered the amount 25.38 Sammy is male This object (or dynamic) variable contains: 9 Now it contains a string value: kitty |
| Previous | Next | Contents | Index |