• Aucun résultat trouvé

i I • END can be placed on a line by itself, or after other state

Dans le document Publishing is (Page 117-123)

ments on a line. This is often done in conjunction with the n IF-THEN statement. It is sometimes desirable for a program

to end early, such as when a potential error has been detected.

• Statements placed after an END statement on the same line p| will never be executed.

n

107

Chapter 8

Controlling

Program

Execution

H

You have seen that when the computer runs a program, it exe cutes the first line, then the second, and so on until there are no more lines, or the END statement is encountered. The lines are executed sequentially, according to the line numbers.

Remember when we introduced conditional logic? We showed how the IF-THEN statement provided control over the execution of statements on one line. Now, with a program, we could use something to provide control over the execution of program lines. This is available with the GOTO statement.

Enter and run the following program. (Be sure to first type NEW if there is an old program in memory. Future examples will not include a reminder.)

10 The computer not only executes the lines in the program, but it keeps on executing them. When the computer sees the state ment GOTO 10 on line 70, it ignores anything after line 70, and continues execution by going back to the first statement of

|—I line 10. This will go on forever, and line 80 never gets executed.

The GOTO statement alters the flow of execution from H the normal course. When GOTO is used to make the computer

execute a bunch of lines several times, the lines form a loop. A loop which continues forever, like the one in the example, is pi called an infinite loop.

! Since the computer executes statements so quickly, it can be difficult to see exactly what the computer is doing when it pi is caught in an infinite loop. To slow down the scrolling, press

Controlling Program Execution

the CTRL key, just as you did to make the computer list pro grams more slowly. At least now you can see what is being printed on the screen. The printing will return to normal speed as soon as you release,, the CTRL key.

A program isn't of much use if it's stuck in an infinite loop. To stop the computer when it's in an infinite loop, or at any time it's executing a program, press the RUN/STOP key.

The computer will immediately stop executing the program, and will print the message BREAK IN followed by a line num ber. The line number indicates which line was being executed when the RUN/STOP key was pressed. The computer will now be in the immediate mode.

The GOTO statement does not have to make the com puter repeat some lines. It can also be used to make execution skip ahead in the program.

10 C=INT(RND(0)*16) :rem 19

20 PRINT "THE NUMBER" C "IS "; :rem 59 30 IF C/2 = INT(C/2) THEN PRINT "EVEN" : GOTO 50

:rem 185

40 PRINT "ODD" :rem 12

50 POKE 53280, C :rem 5

In this example, the GOTO in line 30 is used to bypass line 40. If the number is even, the GOTO 50 will be executed. The only other possibility is that the number is odd, and line 40 handles that. If there were a lot of lines between 30 and 50, the GOTO 50 would skip around all of them.

With just a couple of changes to the program, you can make the computer rapidly change the background and border colors for a psychedelic effect.

30 IF C/2 = INT(C/2) THEN PRINT "EVEN" : POKE 5328

1, C : GOTO 10 :rem 144

60 GOTO 10 :rem 0 , .

The syntax for the GOTO statement consists of the keyword GOTO followed by a line number. This is one case where a

variable, expression, or function cannot be used. Only a line [_j number is acceptable.

You can also spell GOTO with a space between GO and

TO. This is the only keyword in which this is allowed. This is [_

done to maintain compatibility with other versions of BASIC which spell the keyword with a space. The GO TO statement

works just like GOTO. J_J

112

u

n

r- Controlling Program Execution

n

n

Because the GOTO statement is used to move program execution forward or backward to a different line, we say that f—| the GOTO statement makes BASIC jump to a new line.

1 ' GOTO can jump only to the beginning of a line. If the computer has to GOTO a line which contains many state ments, execution will start at the first statement. Also, GOTO should always be the last statement on a line because, just like the END statement, anything after the GOTO will never be executed.

The most common error people make with GOTO is attempting to GOTO a line which is not in the program. When the computer tries to execute the statement GOTO 15 and there is no line 15 in the program, the error message UNDEFD STATEMENT will be printed. This is the com puter's way of telling you that line 15 does not exist. Another way to get this error is to use a variable in place of the line number or to forget the line number altogether in the GOTO statement. In this case the computer will assume a line num ber of 0. If a line 0 exists, the program will jump there; if it doesn't exist, you'll get the error message.

Summary

• The GOTO statement changes the order in which program lines are executed.

• Normally, after the computer executes one line, it will exe cute the next one in the sequence. The GOTO statement causes execution to jump to a different line.

• GOTO can move execution forward or backward in the

program.

• When GOTO is used to skip backward so that lines can be executed again, the lines that are repeated form a loop.

j—] If these lines are executed continuously, without ever

Dans le document Publishing is (Page 117-123)