Regularity
From Maze
(Difference between revisions)
(→The Flow of Control) | (Moved over some additional edited/revised sections) | ||
| Line 51: | Line 51: | ||
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. | ||
| + | |||
| + | === Bounded and Unbounded Loops === | ||
| + | |||
| + | 10 PRINT works as it does because of its repetition of the PRINT statement, repetition that is unconditional: It will continue until something outside the program interrupts it, such as the user pressing the key labeled RUN STOP or unplugging the computer. | ||
| + | |||
| + | At a high level, programs can contain two types of loops: bounded (also called "finite") and unbounded (or "infinite"). 10 PRINT has the latter kind. If one is writing a program that is intended to produce some result and exit, it would be a mistake to include an unbounded loop. The unbounded loop does have legitimate uses, however. It can be used to keep an application, usually an interactive one, running and accepting input until the system is shut off or rebooted. This can be done, and is one, even on a Commodore 64. | ||
| + | |||
| + | Bounded loops are those that end under certain conditions. The exact conditions vary; some will continue until the program reaches a pre-defined exit state while others execute a specific number of times. If there are exit conditions for a loop at all, that implies the programmer was expecting some kind of change to be introduced as the program executes. | ||
| + | |||
| + | A common use of finite loops is to create an iterative process. Iteration is a special type of looping where the result of one pass through the loop influences the result of succeeding passes. The simplest example of an iterative process is nothing more than counting. Beginning with an initial value of 1. Adding 1 to this (that is, increment it) to produce 2. Performing the same incrementing operation again, to yield 3, and so on. If the program goes to some limit, say 10: | ||
| + | |||
| + | <nowiki>10 A=1 | ||
| + | 20 PRINT A | ||
| + | 30 A=A+1 | ||
| + | 40 IF A<=10 GOTO 20 | ||
| + | </nowiki> | ||
| + | |||
| + | The loop is bounded. If line 40 is replaced with 40 GOTO 20, the loop is unbounded. | ||
| + | |||
| + | A conditional branch is what makes loop bounded -- it ends when the condition is met. An unconditoinal branch corresponds to an unbounded loop. | ||
| + | |||
| + | The bounded loop, and counting up or down to a particular value, is so important in programming that BASIC has its own special syntax for specifying that sort of loop, using the FOR ... TO and NEXT statements. The bounded program above could also be written: | ||
| + | |||
| + | <nowiki>10 FOR A=1 TO 10 | ||
| + | 20 PRINT A | ||
| + | 30 NEXT | ||
| + | </nowiki> | ||
| + | |||
| + | === FOR ... TO ... STEP (Sidebar) === | ||
| + | |||
| + | The FOR loop in BASIC is a bit more general than is shown in the example in the main text. It can be used not only to increment a variable, but also to change its value each time by any amount. This is done using the STEP keyword. To show only the even numbers between 2 and 10: | ||
| + | |||
| + | <nowiki>10 FOR A=2 TO 10 STEP 2 | ||
| + | 20 PRINT A | ||
| + | 30 NEXT | ||
| + | </nowiki> | ||
| + | |||
| + | If STEP is omitted, it's the same as adding "STEP 1." The step value can be set to a negative number or even to zero. By setting the value to 0, an unbounded FOR loop can be created: | ||
| + | |||
| + | <nowiki>10 FOR A=1 TO 10 STEP 0 | ||
| + | 20 PRINT "FOREVER!" | ||
| + | 30 NEXT | ||
| + | </nowiki> | ||
| + | |||
| + | This allows for an alternate version of 10 PRINT, slightly longer but still a "one-liner" of no more than 80 characters, that uses a FOR loop instead of GOTO: | ||
| + | |||
| + | <nowiki>10 FOR A=1 TO 2 STEP 0 : PRINT CHR$(205.5+RND(1)); : NEXT | ||
| + | </nowiki> | ||
| + | |||
| + | === Is 10 PRINT Iterating? === | ||
| + | |||
| + | From the discussion so far, it should be clear that 10 PRINT has an unbounded loop thanks to the unconditional branch at the end. There is nothing in the code, however, to indicate that 10 PRINT is an example of iteration. There are no variables like A that change value from one pass to the next -- there are no variables at all, in fact -- and the code inside the loop, and the way that code works, never changes. In the most strightforward sense, the BASIC program <code>10 PRINT CHR$(205.5+RND(1)); : GOTO 10</code> does not iterate. | ||
| + | |||
| + | Watching the program execute on screen shows hints that there is something changing as it runs, though: the position where a new character is displayed changes every time the loop executes, moving one location to the right until it reaches the 40th column; then, the display of characters continues on the next line in column 1. The entire contents of the screen also moves up by two lines, with the top two lines disappearing, once the display of characters reaches the bottom left position. | ||
| + | |||
| + | None of this is a direct result of any BASIC statements included in 10 PRINT. Displaying strings on the screen generally has this effect, since BASIC's PRINT command calls the Commodore 64 KERNAL's CHAROUT routine and produces output in this way. So, while 10 PRINT is not an iterative program, it invokes the iterative behavior of PRINT. In this case, the behvaior is a sort of incrementing, because ";" is used to display the next character immediate after the previous one and a string that is one character long is displayed in each pass. 10 PRINT's simplicity exposes the power of using simple, iterative steps to create a complex construction. | ||
