Regularity
From Maze
(Difference between revisions)
(→Conditional and Unconditional Branching) | (Explaining that GOTO can be used to skip ahead, it isn't inherently a loop command) | ||
| Line 37: | Line 37: | ||
30 PRINT "SOMETHING ELSE..."</nowiki> | 30 PRINT "SOMETHING ELSE..."</nowiki> | ||
| − | If, after running this program, the user types just the digit "1" and then presses RETURN, all of the statements on line 20 will be executed. "YOU TYPED ONE!" will be printed and then the program will terminate as END instructs. If the user types anything else before pressing RETURN, the flow of control moves to line 30; both the first PRINT statement and the END are skipped. "SOMETHING ELSE..." is printed instead. | + | If, after running this program, the user types just the digit "1" and then presses RETURN, all of the statements on line 20 will be executed. "YOU TYPED ONE!" will be printed and then the program will terminate as END instructs. This is another way to change the flow of the program, of course: use END or STOP to terminate the program. If STOP is used, the CONTINUE command can be issued to pick up where the program left off. |
| + | |||
| + | If the user types anything else before pressing RETURN, the flow of control moves to line 30; both the first PRINT statement and the END are skipped. "SOMETHING ELSE..." is printed instead. | ||
| + | |||
| + | This program, although written differently, does exactly the same thing: | ||
| + | |||
| + | <nowiki>10 INPUT A$ | ||
| + | 20 IF A$ = "1" THEN GOTO 40 | ||
| + | 30 PRINT "SOMETHING ELSE..." : END | ||
| + | 40 PRINT "YOU TYPED ONE!"</nowiki> | ||
| + | |||
| + | Instead of using the IF ... THEN to directly determine whether two statements (the PRINT and END statements) should be executed, this one changes the flow of control with GOTO. The GOTO statement is used to skip ahead past PRINT "SOMETHING ELSE..." and END to line 40. Although this isn't a very exciting program, it shows that unconditional branching can be used to move ''ahead'' in the sequence of lines; there is nothing about GOTO that means it ''must'' be used to repeat or loop. | ||
Although there is no IF ... THEN statement in 10 PRINT, and the program does not by any interpretation contain a conditional branch, this short program does accomplish a very small-scale sort of variation. By computing 205.5+RND(1) and passing that value to the function CHR$, the program prints either PETSCII character 205 or PETSCII character 206. This variation between two characters is a one-bit variation, a selection from the smallest possible set of options. Yet, in combination with the regularity of a repeating processes and the regularity of the Commodore 64's screen grid, these selections take shape as something evocative and visually interesting. | Although there is no IF ... THEN statement in 10 PRINT, and the program does not by any interpretation contain a conditional branch, this short program does accomplish a very small-scale sort of variation. By computing 205.5+RND(1) and passing that value to the function CHR$, the program prints either PETSCII character 205 or PETSCII character 206. This variation between two characters is a one-bit variation, a selection from the smallest possible set of options. Yet, in combination with the regularity of a repeating processes and the regularity of the Commodore 64's screen grid, these selections take shape as something evocative and visually interesting. | ||
