 # Jonathan E. Sisk's Pick/BASIC: A Programmer's Guide

## Chapter 5 - Calculations and the Principle of Precedence

In program example 3 the principle of precedence is discussed and several of the intrinsic functions related to numbers are covered. This program also includes a simple guessing game, which further illustrates the principle of decision points and branching within a program. Topics, statements, and functions covered include precedence, RND and REM.

Enter Program Example 3 (Fig. 5-1).

Fig, 5-1. Program Example 3.

001 * EX.003
002 * PRECEDENCE OF MATH OPERATIONS AND A FEW MATH FUNCTIONS
004 * JES: author's initials
005 *
006 PROMPT ":"
007 *
008 * SHOW THE EXPRESSION WITHOUT PARENTHESES
009 *
010 PRINT
011 PRINT "HERE IS WHAT HAPPENS WHEN WE RELY ON PRECEDENCE "
012 PRINT
013 PRINT "10 + 20 * 5 - 12 / 3 =" : 10 + 20 * 5 - 12 / 3
014 *
015 * NOW SHOW IT WITH PARENTHESES
016 *
017 PRINT
018 PRINT "HERE IS WHAT HAPPENS WHEN WE PARENTHESIZE EXPRESSIONS : "
019 PRINT
020 PRINT "((((10 + 20) * 5) - 12) / 3) =": ((((10+20) * 5) - 12)/3)
021 *
022 * GET NUMBERS FOR DIVISION TEST
023 *
024 PRINT
025 PRINT "ENTER A NUMBER TO DIVIDE" :
026 INPUT NUMERATOR
027 IF NUMERATOR = "QUIT" THEN STOP
028 *
029 PRINT "ENTER NUMBER TO DIVIDE BY" :
030 INPUT DENOMINATOR
031 IF DENOMINATOR = "QUIT" THEN STOP
032 *
033 PRINT
034 PRINT NUMERATOR: "DIVIDED BY" : DENOMINATOR:
035 PRINT "LEAVES A REMAINDER OF" : REM(NUMERATOR,DENOMINATOR)
036 *
037 * NOW, LET'S PLAY GUESSING GAME...
038 *
039 MY.NUMBER = RND(10) + 1 ; * GENERATE THE RANDOM NUMBER
040 PRINT
041 PRINT "I HAVE A NUMBER BETWEEN ONE AND 10"
042 PRINT "TRY TO GUESS WHAT IT IS"
043 *
044 * MAKE USER GUESS NUMBER
045 *
046 LOOP
047 PRINT "ENTER YOUR GUESS" :
048 INPUT GUESS
049 IF GUESS = "QUIT" THEN STOP ;* MUST HAVE GIVEN UP
050 UNTIL GUESS = MY.NUMBER DO
051 PRINT "SORRY. THAT'S NOT IT. TRY AGAIN"
052 REPEAT
053 *
054 *
055 PRINT
056 PRINT "CONGRATULATIONS. YOU GOT IT"
057 END

### ARITHMETIC OPERATIONS AND PRECEDENCE

Expressions are evaluated in a program in accordance with the rules of precedence. The highest precedence is parentheses. When parentheses are present in an expression, operations within the innermost set of parentheses have the highest precedence. The second highest priority is exponentiation. Multiplication and division both comprise the third level.

When two functions of the same level of precedence occur in an expression, they are evaluated from left to right. The fourth level is addition and subtraction (with the same left-to-right evaluation scheme). Level five in the Pick System, is "print masking," followed on level six by concatenation. Level seven is for relational operators (such as ">" for "greater than' '), and finally, on level eight, are the logical operators AND and OR.

Two operators may not be used in succession unless they are separated by parentheses. For example, the expression:

X^-Y

will not even compile, much less work. It must be written as:

x ^ (-Y)

Use the higher precedence of parentheses to overcome situations where two operations of the same level, such as multiplication and division, occur in an expression. Table 5-1 summarizes the precedence of operations in Pick.

On line 13 of the example, the result of the calculation is printed. This results in the answer "106." Line 20 provides the result "46" because precedence has been altered through the use of parentheses.

013 PRINT "10 + 20 * 5 - 12 / 3 =": 10 + 20*5 - 12/3

As a matter of style, and to ensure accuracy in mathematical expression, use parentheses when more than one arithmetic operator appears in an expression.

Table 5-1, Precedence of Mathematical Expressions

```Operator Operation       Sample in Pick/BASIC
^        Exponentiation  X ^ Y
*        Multiplication  X * Y
/        Division        X / Y
-        Subtraction     X - Y
:        Concatenation   X : Y
=        Relational      X = Y
>, <     Relational      X > Y
>=, <=   Relational      X >= Y
#, <>,   Relational      X # Y
><       Relational      X >< Y
AND / OR Logical         X < Y AND Z > 0
```

### THE REM FUNCTION

The REM function returns the remainder of a numeric expression divided by a second numeric expression:

035 PRINT "LEAVES A REMAINDER OF " : REM(NUMERATOR,DENOMINATOR)

REM also happens to be one of the few functions in PICK/BASIC where there is potential ambiguity. This is due to the fact that there is also a REM statement, which is an alternate means of declaring a REMark statement.

When the REM appears as the beginning of a statement, the compiler interprets it as a remark statement, the same as the * and ! characters. For example:

REM Get user response and determine if valid

Otherwise, it is interpreted as a remainder function. For example:

PRINT REM(TOTAL.AMOUNT,2)

or

### THE RND FUNCTION

The RND function generates a random integer number whose value is between zero and the numeric expression in the parentheses, minus 1.

039 MY.NUMBER = RND(10) + 1; * GENERATE THE RANDOM NUMBER

In line 39, the RND function would first generate a random number between 0 and 9 (which is 10 minus 1); then 1 is added to the random number, and the result is then stored in the variable MY.NUMBER. This means that MY.NUMBER is now a number between 1 and 10, inclusively.

The RND function is particularly useful for determining amounts of salary increases.

Note that the guessing game program in the example is extremely forgiving. It keeps prompting until you provide either the correct answer or QUIT.

# REVIEW QUIZ 3

1) Why is precedence important?

2) What is the difference between the REM statement and the REM function?

3) What does the RND function do?

4) What is wrong with the following program samples and how may they be corrected?

```     a) IF ANS = "Y" THEN PRINT "YES" END ELSE PRINT "NO"

b) IF ANSWER > 0 THEN
ELSE
END

c) IF ANSWER = "N" THEN
PRINT "ENTER ALTERNATE VALUE" :
INPUT ALTERNATE.VALUE
IF ALTERNATE.VALUE = "" OR ALTERNATE.VALUE <= 0 THEN
PRINT "MUST BE A NUMBER OR POSITIVE !"
END
``` Previous chapter Next chapter Top 