Sheerpower®
A Guide to the Sheerpower Language


Previous Contents Index

4.8.3 Conditional Numeric Expressions

Relational operators are similar to those used in algebra. The relational operators are:


 
=      equals                           X=Y           X is equal to Y 
 
<      less than                        X<Y           X is less than Y 
 
>      greater than                     X>Y           X is greater than Y 
 
<=     less than or equal to            X<=Y          X is less than or equal 
                                                        to Y 
 
>=     greater than or equal to         X>=Y          X is greater than or 
                                                        equal to Y 
 
<>     not equal to                     X<>Y          X is not equal to Y 
 

X and Y can be any unconditional or conditional expression.

4.9 Performing Relational Operations on Strings

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-12 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".

4.9.1 Logical Operators

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   | 
                      +--------------------------+ 

4.9.2 Bit Manipulation

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-13 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 

4.10 Order of Evaluation

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:

  1. Sheerpower always evaluates expressions in parentheses first. Parentheses, ( ), can be used to change the order of any of the following operations. If parentheses are nested, Sheerpower evaluates them from the inside out. For example:


                    Z%-(X% / (Y% + AMOUNT)) 
    

    Sheerpower evaluates the expression Y% + AMOUNT first. Next, it divides the X% by AMOUNT to determine that result. Finally, it subtracts the entire sum from Z%.

  2. Sheerpower performs functions second.
  3. Sheerpower performs exponentiation.
  4. Sheerpower performs multiplication and division.
  5. Sheerpower performs addition and subtraction.
  6. Sheerpower performs relational operations from left to right. (The relational operators are: =, <, >, <=, >= and <>.) The only exception is the assignment of the result. The result is always assigned last.
  7. Sheerpower performs logical operations in the following order:
    NOT
    AND
    OR
    XOR
    IMP
    EQV

4.11 Using Parentheses for Clarity

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.

Example 4-14 Use of Parentheses for Clarity in Operations

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 


Previous Next Contents Index