Chapter four: Loops and branches

Scan of page 27 Scan of page 28 Scan of page 29 Scan of page 30 Scan of page 32 Scan of page 32

Conditional statements

The IF statement

This condition takes the form

IF condition action

If the condition is true the action is performed, otherwise the next statement is obeyed. One of two types of action may follow an IF statement:

  1. The program may be directed to another line with a statement such as:

       10 ΙF Α > 20 ΤΗΕΝ 200
    Or 10 IF A > 20 GO TO 200
    

    which are exactly equivalent. If the line number 200 did not exist, the program run would be terminated. lf line 200 contained a non-executable statement such as REM, DATA, DEF or DIM, control would pass to the first line after 200 that contained an executable statement.

  2. A subsidiary statement could follow the IF. This may be any statement other than DATA, DEF, DIM, END, FNEND, FOR, NEXT and REM.

    e.g.:

    10 IF A = B PRINT C
    

Logical conditions

These may take several forms.

  1. numeric-expression relation-operator numeric-expression

    The relation operator may be:

    < or = or >
    

    or any two of these together e.g.:

    >=
    

e.g.:

IF (X+1) > (Y/2) GOTO 300
  1. string-expression relation-operator string-expression

e.g.:

IF P$ = "CEDRIC" PRINT "GOOD MORNING"

a) & b) above are called logical terms and may be used alone or linked together using logical operators.

The logical operator may be AND, OR, XOR, IMP, EOV (AND, OR, EXCLUSIVE OR, IMPLICATION, EOUIVALENCE) which have the following truth tables.

A B A AND B A OR B A XOR B A IMP B A EQV B
T T T T F T T
T F F T T F F
F T F T T T F
F F F F F T T

e.g.:

IF (X + 1) > (Y/2) AND P$ = "CEDRIC" INPUT A$

This will only give the answer TRUE if both logical terms are true.

A logical term may also be

  1. NOT logical term:

    e.g. NOT A = B
    

    This will only be true if A is NOT equal to B.

  2. (logical condition)

    That is any of the forms mentioned above in brackets:

    e.g. NOT A = B OR (X + 1 > Y/2 AND P$ = "CEDRIC")
    

    The part in brackets is always evaluated first. The condition will be true if either the first part or the second part is true.

Note on relation operators

If the relation operator consists of two symbols then it is as if the condition were duplicated, once for each of the symbols with an OR between them

e.g.:

А => В

is equivalent to A = B OR A > B (which is also equivalent to NOT A < B)

In relations between strings < (less than) is interpreted as “earlier in the alphabet than” and > (greater than) as “later”.

e.g.:

"CAR" < "CARD" < "CART HORSE" < "CARTER" < "CAT"

Logical conditions are evaluated in the following order:

parentheses (brackets)
numeric and string expressions
relation operators (> = <)
NOT
AND
OR
XOR
ΙΜΡ
EOV

Examples

40 ΙF Χ + 1 => 17 ΤΗΕΝ 150
50 IF P$ = "FRED" PRINT P$
60 IF X = 1 AND Y = 1 GOTO 170
70 IF P$ = "FRED" OR NOT O$ = "HARRY" THEN 180
80 IF NOT (X = 1 AND Y < 1) GOTO 190
90 ΙF (Α ΜΑΧ Β) > 10 ΤΗΕΝ 200

ON . . . GOTO

The ON statement will transfer control of the program to one of a selection of statements or subroutines.

It takes the form of:

ON numeric-expression (GOTO) statement number list
                      (THEN)
                      (GOSUB)

e.g.:

60 ΟΝ Χ GΟΤΟ 140, 180, 220

X will be rounded to the nearest integer and control will be transferred to 140, 180 or 220 depending on whether X = 1, 2 or 3.

If X 3< 1 or X > 3 then control will pass to the next statement after the ON statement.

Subroutines

Subroutines are self contained sections of program. They are accessed using the GOSUB statement.

e.g.:

100 GOSUB 2000

This will transfer control to the specified statement number. On encountering a RETURN statement control will be returned to the statement following the GOSUB.

e.g.:

100 GOSUB 2000
11Ο RΕΜ CΑRRΥ ΟΝ
  .
  .
  .
2000 PRINT "**************"
2010 PRINT "ERROR"
2020 PRINT "**************"
2030 RETURN

Subroutines are useful for frequently used pieces of code. Instead of writing a set of instructions many times it is written just once saving time and storage space.

Subroutines use the same variables and data areas as the main programs, and have no parameters.

FOR-NEXT loops

Simple FOR-NEXT Loops are introduced in Chapter two. In FOR statements the STEP may be fractional or negative or both.

e.g.:

100 FOR X = 10 TO 1 STEP - 0.5

If the STEP is negative the test X < limit is made (e.g. X < 1)

X will be set equal to 10, 9.5, 9, 8.5. . .2, 1.5, 1, 0.5

When X = 0.5 the test X < 1 is TRUE and the loop ends.

The initial and final values for X may also be negative or fractional or both.

e.g.:

100 FOR I = -3 TO -5.75 STEP -0.25

If an impossible loop has been asked for

e.g.:

100 FOR N = 1 TO 4 STEP - 1

N will be set to the initial value specified and then tested against the final value. In the above case the STEP is negative therefore the test will be N < limit i.e. N(= 1) < 4 which is true. So control will be passed immediately to the statement following the corresponding NEXT. Note that N remains set to the initial value.

At the end of any FOR Loop the value of the variable will be the value which satisfied the condition.

e.g.:

100 FOR X = 1 TO 10

X will equal 1, 2, 3,...... , 9, 10, 11.

When X = 11 the test X > 10 is true.

X retains the value 11.

FOR statements may be nested within each other

e.g.:

100 FΟR Χ = 1 ΤΟ 3
110 FΟR Υ = 1 ΤΟ 3
120 PRINT A (X,Y)
130 ΝΕΧΤ Υ
140 ΝΕΧΤ Χ

The inner loop will run three times for each value of X, so that the following will be printed:

Α(1,1)
A(1,2)
Α(1,3)
Α(2,1)
A(2,2)
A(2,3)
A(3,1)
A(3,2)
A(3,3)

Care should be taken to make sure the NEXT statements are always present to close each loop, and that they are in the right order.

e.g.:

100 FΟR Χ = 1 ΤΟ 3
110 FΟR Υ = 1 ΤΟ 3
200 ΝΕΧΤ Χ
210 ΝΕΧΤ Υ

is incorrect.

Function definitions

It is possible to define functions using the DEF statement. Certain functions such as SOR (to find a square root) or SIN (to find the sine) are already set up.

The DEF statement can be used to define a function

e.g.:

40 DΕF FΝΑ(Χ) = Χ*Χ + 1

From then on in the program to calculate, for example, P2 + 1

100 LET B = FNA (P)

may be used.

The DEF statement can also be used to introduce a function definition block which is a set of statements terminated by an FNEND statement.

This form would be used when the function definition required more than one statement line. The result of a function definition should be a single value - either numeric or string.

All function names take the form FNvariable name. It should be indicated in the function name whether the result is a numeric value or a string.

e.g.:

FNX for a numeric value
FNA$ for a string value.

A function block may contain any statements other than DEF, FNEND or END. Any FOR-NEXT statements must be matched within the block.

It is not possible to jump into or out of a function definition block.

Parameters and local variables

Functions may be defined with or without parameters and with or without local variables. These exist only while the function is being evaluated, thus it is quite permissible to use the same variable names within a definition as outside without fear of corruption.

When a function is used the parameters must be the same in number and type as those defined.

e.g.:

40 DEF FNA (X, YŞ)
 .
 .
90 FNEND
 .
 .
200 LET B = FNA (P, O$)

Local variables can be defined for use within a function definition block only. If a variable is used which is not locally defined it is taken to be a main program variable. In the example below A and P$ are local variables.

e.g.:

40 DEF FNB (X, Y, Z$) A, P$
50 LET P$ = Z$ & Z$
60 LET A = LEN (P$)
70 ΙF Α > Χ + Υ ΤΗΕΝ 100
80 FNB = X + Y
90 GΟΤΟ 110
100 FNB = A
110 FNEND
 .
 .
400 REM FUNCTION CALL
410 LET Q = FNB (V, W, T$)

Local variables are undefined at the start of the evaluation, even if RUN CLEAR is used.

It is possible to define a recursive function. See the 2903 BASIC: Reference manual for further information.