CHAPTER 4

LOOPS AND BRANCHES


A loop is a section of program that tells the Sorcerer to repeat an action a certain number of times. A branch is a place in a program where the Sorcerer must make a decision, and execute different parts of the program according to that decision.


THE GO TO STATEMENT


This statement tells the Sorcerer to go directly to a certain line in the program. The format is:

GO TO < line number>

(Most programmers write it GOTO, without the space between the words.)

The GOTO statement is an unconditional branch. You can use the GOTO statement to send the Sorcerer to any line of a program, even to a REM instruction, but there must be a line corresponding with the <line number> or you get a UL (undefined line) error message on execution.

Example:

10 PRINT "ECHO"
20 GOTO10

This short program will run forever, on an endless loop, until you reset the Sorcerer, turn it off or until you stop the program with CTRL C.


THE FOR ... NEXT LOOP


The FOR ... NEXT loop starts with a FOR statement and ends with a NEXT statement. In between, you can write any instructions you like--even another FOR ... NEXT loop.

The FOR statement has this form:

FOR < variable > = < first value > TO < final value > [ STEP< step value> ]

The NEXT statement is written:

NEXT [ <variable> ] [ ,<variable>... ]

With one variable in the FOR statement, you may use this form:

NEXT

Examples:

10 FOR X=1 TO 10     This loop prints the intergers
20 PRINT X 1 to 10 (one number per line for
30 NEXT ten lines)

10 FOR X=1 TO 10 This loop prints the
20 PRINT X; integers 1 to 10
30 NEXT (all on the same line)

100 FOR Y=1 TO 10 This loop prints the letter
110 PRINT "A" A 10 times 120 NEXT Y

60 FOR A=-10 TO 5 This loop prints the 16
:PRINT A;:NEXT intergers from -10 to 5 including 0

When the Sorcerer sees a FOR instruction, it assigns the first value to the variable. It then steps through each of the following instructions until it reaches the last statement of the loop (the NEXT statement). It then increases the value of the variable by the STEP value, and compares the result to the final value. (We'll get to STEP in a moment. If the STEP option is not used, the variable increments by 1 each time through the loop. 1 is the default value.) If the new value is more than the final value, the Sorcerer continues to the instruction after the NEXT statement. Otherwise it jumps back to the FOR statement and goes through the instructions in the loop again. Eventually the variable becomes larger than the final value, and the loop ends.

Example:

You type:
100 PRINT "I WILL COUNT FROM 1 TO 3 FOR YOU."
200 FOR X=1 TO 3
300 PRINT X
400 NEXT X
500 PRINT "NOW I AM DONE COUNTING."
RUN
Sorcerer replies:
I WILL COUNT FROM 1 TO 3 FOR YOU.
1
2
3
NOW I AM DONE COUNTING.

Sorcerer's actions.

  1. PRINT the message (line 100).
  2. Set X to 1.
  3. Compare the present value of X with 3. 1 is not greater than 3, so execute the instructions following the FOR statement.
  4. PRINT the present value of X (=1).
  5. Set X to 2 (by adding 1).
  6. Compare the present value of X with 3. 2 is not greater than 3, so execute the instructions following the FOR statement.
  7. PRINT the present value of X (=2).
  8. Set X to 3 (add 1).
  9. Compare the present value of X with 3. 3 is not greater than 3, so execute the instructions following the FOR statement.
  10. PRINT the present value of X (=3).
  11. Set X to 4 (add 1).
  12. Compare the present value of X with 3. 4 is greater than 3, so go to line 500.
  13. PRINT the message (line 500).

The first and final values (and the STEP value) do not have to be specific numbers; you can use numerical expressions (variables). When the Sorcerer first sees the FOR instruction, it computes the values of the expressions and uses those values as the first and final values for the loop variable.

Example:

25 FOR X=C to 100
30 PRINT X+C
35 NEXT X

105 FOR A=X TO Y
110 PRINT "A"; 115 NEXT

This program asks for the first and final values and then counts for you:

10 INPUT "WHERE SHALL I START";A
20 INPUT "WHERE SHALL I END";B
30 FOR Z=A TO B
40 PRINT Z;
50 NEXT Z

(Do not forget to enter a RETURN after each input.) If the value you type in for B is smaller than that for A, or the same, only the first value will be printed. If a decimal value is used for A, the program increments each time by 1 until it reaches the next value lower than B. Sample runs might look like this:

RUN

WHERE SHALL I START?-7

WHERE SHALL I END?3

-7 -6 -5 -4 -3 -2 -1  0  1  2  3


RUN

WHERE SHALL I START?100

WHERE SHALL I END?50

100


RUN

WHERE SHALL I START?3.1416

WHERE SHALL I END?6

3.1416  4.1416  5.1416

You can also have the Sorcerer increase the FOR variable by other than 1, or even decrement it. To do so, use this FOR statement:

FOR <variable> = <first value> TO <final value> STEP <step value>

The <step value> can be a number, positive or negative, or a numerical expression. (Using 0 as a step puts your program into an infinite loop, endlessly executing the first step.)

Example:

This program prints all the odd numbers (skipping the even ones) from 1 to 100. Note that the last number printed will be 99. In a FOR ... NEXT loop the Sorcerer gets as close to the final number as it can without exceeding that number.

10 FOR Z=1 TO 100 STEP 2

20 PRINT Z

30 NEXT

This program counts backwards from 10 to 1 in steps of 0.1 (10, 9.9, 9.8, 9.7, etc.)

110 FOR A=10 TO 1 STEP -.1

120 PRINT A

130 NEXT A

This program asks you to tell it where to begin counting, where to end and in what steps you wish.

100 INPUT "START AT"; A

110 INPUT "END WITH"; B

120 INPUT "STEPS OF"; C

130 FOR X=A TO B STEP C

140 PRINT X;

150 NEXT X

If B is greater than A then C should be positive; if B is less than A then C should be negative. Try running the program with various inputs.

Try running this program:

10 PRINT "COUNTDOWN"

20 PRINT

30 FOR X=10 TO 1 STEP -1

40 PRINT X

50 PRINT

60 NEXT X

70 PRINT "BLASTOFF!"

This program zips by too fast. To slow it down, insert these lines into your COUNTDOWN program:

52 FOR Y=1 TO 130

54 NEXT Y

This is called a delay loop. It forces the Sorcerers to count from 1 to 130 during each pass through the FOR X loop; this slows down the program so that the printed numbers move more slowly up the screen (about one second between counts).

If you put one FOR ... NEXT loop inside another FOR ... NEXT loop, you should use the longer form of the NEXT statement:

NEXT <variable>

Otherwise the Sorcerer will be confused about which NEXT statement ends which loop, and you may get an NF error message ("next without for").

You can sometimes use a single NEXT statement to end two or more loops. For example, in the COUNTDOWN program you could replace

54 NEXT Y

60 NEXT X

with

60 NEXT Y,X

Until you thoroughly understand how FOR ... NEXT loops work, use a separate NEXT statement to end each FOR ... NEXT loop. That way you avoid confusing the Sorcerer (and yourself). If you do thoroughly understand the preceding, then read on.

When FOR ... NEXT loops are "nested," that is, when one or more loops lie within one another, then the NEXT <variable> form must be used on all but the outermost NEXT, with the variables appearing in reverse order from that used in the FOR statements.

Example:

10 FOR X=1 TO 3

20 FOR Y=1 TO 3

30 FOR Z=1 TO 3

40 PRINT X,Y,Z;

50 PRINT

60 NEXT Z

70 NEXT Y

80 NEXT X

These three nested loops produce a table of 27 lines (3 x 3 x 3)of three numbers each. Try it. Lines 60, 70 and 80 could be combined in this way:

60 NEXT Z:NEXT Y:NEXT X

or this way:

60 NEXT Z,Y,X

In the first two cases, the last X could be eliminated, as:

60 NEXT Z:NEXT Y:NEXT

or

60 NEXT Z

70 NEXT Y

80 NEXT

THE IF ... THEN STATEMENT


Let's play a game. You pick an integer from 1 to 10 and the Sorcerer will guess what your number is.

10 REM GUESSING GAME

20 PRINT "THINK OF A NUMBER FROM 1 TO 10"

30 PRINT "I WILL TRY TO GUESS YOUR NUMBER"

40 PRINT "IF I GUESS CORRECTLY, TYPE 1."

50 PRINT "IF I GUESS INCORRECTLY, TYPE 2."

60 FOR X=1 TO 10

70 PRINT "IS YOUR NUMBER";X;

80 INPUT Y

90 IF Y=1 THEN 120

100 PRINT

110 NEXT X

120 PRINT "I KNEW IT ALL ALONG."

130 PRINT

140 GOTO 20

REMARK

Notice the use of the = sign here. This is the mathematical use you recognize.


The IF ... THEN instruction (line 90) tells the Sorcerer to decide whether or not a certain logical expression (Y=1) is "true". If the expression is true, then the Sorcerer continues to the instruction after the THEN (which, in this case, tells it to jump to line 120). If the expression is "false", that is if Y is not equal to 1, then the Sorcerer goes to the next line (line 100, here). (This program repeats endlessly untill you get tired of it, at which point you type CTRL C.)

The general format for this kind of IF ... THEN statement is"

IF <expression> THEN <line number>

This is a form of the IF ... THEN statement known as the numerical IF ... THEN statement. When the Sorcerer comes to such an instruction, it evaluates the numerical expression. If the expression has the value 0, then the Sorcerer just moves on to the next instruction. But if the expression has any value other than 0, it continues to the instruction following THEN. (To a computer, an expression that evaluates to 0 is "false"; one that evaluates to anything else is "true"). In numerical IF ... THEN form, line 90 of the previous program could be changed to 90 IF Y-2 THEN 120. If your response to the question in line 70 is 2, then the expression Y-2 evaluates to 0 and the Sorcerer goes to the next line, 100. If your response is 1, i.e., a correct guess, then the expression evaluates to something other than 0, and the Sorcerer looks at what comes after THEN, which, in this case, is an instruction to go to line 120. Here is another example of this numerical IF ... THEN:

10 INPUT A,B

20 IF A*A + B*B THEN 40

30 PRINT "BOTH A AND B ARE ZERO."

40 PRINT "END OF PROGRAM."

If you enter 0 for both A and B, then the expression A*A + B*B has value 0, causing the Sorcerer to go on to the next instruction and print BOTH A AND B ARE ZERO before going on to line 40. Entering any other value than 0 for either A or B gives the expression a non-zero value and causes the Sorcerer to skip past line 30 and print END OF PROGRAM.

Another kind of IF...THEN statement looks like this:

IF < expression > THEN < statement >

The <statement> can be any Standard BASIC program statement (including another IF ... THEN statement).

When the Sorcerer comes to this IF ... THEN instruction, it evaluates the < expression >, and moves to the next line if the value is false (or 0). If the value is true (or not 0), it executes the <statement>, and then goes on to the following instruction.

Example:

10 INPUT "PICK A NUMRER";X

20 IF X<0 THEN PRINT "YOUR NUMBER IS NEGATIVE"

30 PRINT "END OF PROGRAM"

When the Sorcerer evaluates the expression (X<0) and finds it to be false the program drops immediately to line 30 and does not look at the statement following THEN. If the Sorcerer finds the expression to be true, it executes the statement following THEN (PRINT "YOUR NUMBER IS NEGATIVE") and only then does it continue to line 30. Two sample runs show what happens with different inputs:

RUN

PICK A NUMBER? 3

END OF PROGRAM


RUN

PICK A NUMBER? -3

YOUR NUMBER IS NEGATIVE

END OF PROGRAM

Example:

10 INPUT "ENTER TWO NUMBERS";X,Y

20 IF X=Y THEN PRINT "X=Y":GO TO 10

30 PRINT "X AND Y ARE DIFFERENT":GO TO 10

If you enter two different numbers for X and Y, the Sorcerer prints X AND Y ARE DIFFERENT; otherwise it prints X= Y. In both cases the program returns to line 10.

Notice that in this type of IF...THEN statement a line may contain more than one statement. In the IF... THEN <line number> form it may not. In the following program, the message IT NEVER GETS HERE never prints:

10 INPUT A

20 IF A=0 THEN 40:PRINT "IT NEVER GETS HERE"

30 PRINT "IT GETS HERE IF A IS NOT 0"

40 PRINT "END OF PROGRAM"

The FOR ... NEXT loop is a simpler way of doing a program than a combination of unconditional branching and IF ... THEN type of comparison. Run this program and you will see that the result is the same as the simple counting program you encountered earlier:

100 PRINT "I WILL COUNT FROM 1 TO 3 FOR YOU"

200 X=1

300 PRINT X

400 X=X+1:IF X<=3 THEN 300

500 PRINT "NOW I AM DONE COUNTING."

(You will learn more in Chapter 7 about relation operators. "<=" is a relation operator. It means "less than or equal to," so the second statement in line 400 is saying, "if X is less than 3 or equal to 3 then go to line 300.")

The line numbers are the same as the previous program that counts from 1 to 3. Here is the logic. When the Sorcerer sees the first assignment statement, it gives the variable that first value. It then steps through each of the following instructions until it reaches the statement that increments the variable. With an IF ... THEN statement it then compares the result to a final value. If the new value is more than the final value, the Sorcerer continues to the instruction in the next line. Otherwise it is instructed to go back to the statement after the original assignment statement and it goes through the instructions in the loop again. Eventually the variable becomes larger than the final value tested for in the IF ... THEN statement, and the loop ends, as before.

As before, when you run the program, the Sorcerer replies:

I WILL COUNT FROM 1 TO 3 FOR YOU.

1

2

3

NOW I AM DONE COUNTING.

Sorcerer's actions:

actions:

  1. PRINT the message (line 100).
  2. Set X to 1.
  3. PRINT the present value of X (=1).
  4. Add 1 to X (line 400). See if X is less than or equal to 3. It is, so go to line 300.
  5. PRINT the present value of X (=2).
  6. Add 1 to X (line 400). See if X is less than or equal to 3. It is, so go to line 300.
  7. PRINT the present value of X (=3).
  8. Add 1 to X (line 400). X is now 4, which is not less than or equal to 3, so go to line 500.
  9. PRINT the message (line 500).

END AND STOP


Suppose you cheat at the guessing game(in the IF ... THEN section). You might pick a number outside the range 1 to 10, you might not choose an integer or you might lie when the Sorcerer guesses your number. If you cheat, the Sorcerer will go through each of the numbers 1 to 10 and not get a response from you on any of them.

Add these lines to the program:

112 PRINT "YOU CHEATED. I DON'T PLAY WITH CHEATERS."

115 STOP

The STOP instruction tells the Sorcerer to stop the program, even though it hasn't come to the last instruction. It returns to command mode and prints the message BREAK IN LINE nnnn, where nnnn is the number of the line with the STOP instruction (here 115). Use a STOP because you do not want a program to continue past a certain point. In the guessing game, after the Sorcerer has made ten guesses and not received a 1 response, you do not want it to print the message I KNEW IT ALL ALONG.

You could get the same effect by replacing line 115 (the STOP instruction) with:

115 G0 TO 150

and adding

150 END

The END instruction tells the Sorcerer that it has reached the end of the program and should therefore stop. You don't usually write an END statement in programs for the Sorcerer because the Sorcerer always internally adds one to the end of your program. (You don't see the statement, but the Sorcerer assumes it is there if you don't use it.) Only in special cases, like the preceding, do you use an END statement.

Many other versions of BASIC, by the way, require that the last line in any program be an END statement.

Notice that in the guessing game, the normal branch of the program (the non-cheater's branch) never reaches the END statement, because of the GOTO instruction at line 140.

A STOP or END statement can come anywhere in your program; when the Sorcerer comes to a STOP, it prints a message telling you at what line it has stopped. The program and all variable values remain in memory and you can continue the program with the command CONT (see Appendix B). Use an END statement if you don't want to get the

BREAK IN nnnn

message. That is, you could replace line 115 above with

115 END

and you don't need a line 150.


Table of Contents | Prev | Next

The Trailing Edge