Regularity
From Maze
(Difference between revisions)
(→GOTO and the Unbounded Loop) | (New lead-in) | ||
| Line 1: | Line 1: | ||
= The Grid = | = The Grid = | ||
| − | = Iteration = | + | = Repeating: Regularity in Time and Process = |
| − | |||
| − | ''Perhaps the single most important programming idea is that of a loop.'' | ||
| − | |||
| − | - ''BASIC: A Manual for BASIC, the elementary algebraic language designed for use with the Dartmouth Time Sharing System,'' 1 October 1964 | ||
=== The Flow of Control === | === The Flow of Control === | ||
| Line 18: | Line 14: | ||
SECOND</nowiki> | SECOND</nowiki> | ||
| − | Because the command on the left is executed first; then, the command on the right is carried out. In executing the following program, the topmost, leftmost command is run first, then the one to the right, and then the one on the next line, printing three lines that read FIRST, SECOND, and THIRD: | + | The command on the left is executed first; then, probably not very surprisingly, the command on the right is carried out. In executing the following program, the topmost, leftmost command is run first, then the one to the right, and then the one on the next line, so that three lines that read FIRST, SECOND, and THIRD are printed: |
<nowiki>10 PRINT "FIRST": PRINT "SECOND" | <nowiki>10 PRINT "FIRST": PRINT "SECOND" | ||
20 PRINT "THIRD"</nowiki> | 20 PRINT "THIRD"</nowiki> | ||
| − | The order in which lines, such as the two previous lines, are actually typed in by the programmer is completely irrelevant to how the program runs. The lines of a BASIC program are effectively sorted based on their line numbers before they are run. | + | The order in which these two lines are actually typed in by the programmer is completely irrelevant to how the program runs. The lines of a BASIC program are executed based on their line numbers, not based on when lines were entered. |
There are some important ways to complicate this straightforward program flow. All of these ways involve branching, which causes program flow to continue at some other command rather than what would is, according to left-to-right, top-to-bottom program structure, the next one. | There are some important ways to complicate this straightforward program flow. All of these ways involve branching, which causes program flow to continue at some other command rather than what would is, according to left-to-right, top-to-bottom program structure, the next one. | ||
| − | In BASIC, the IF ... THEN ... statement is used to branch conditionally, so that different code is executed depending upon the value of an expression: | + | === Conditional and Unconditional Branching === |
| + | |||
| + | At a low level, an executing program can either continue to process the next instruction or can move out of sequence to process a different instruction. Just as a reader of a text will typically move to the next word or line, the usual case in the execution of a program is to move to the next instruction. This involves incrementing the program counter so that it points to the place where code is located in memory. If the current instruction is one that can change the flow of control, however, the program may jump or branch to a new memory location and a new piece of code. | ||
| + | |||
| + | There are two ways to branch. An unconditional branch always directs the program to jump to a new location, like the instruction "Go directly to Jail -- do not pass Go" in Monopoly. The assembly mnemonic for an unconditional branch is JMP; the corresponding BASIC keyword is GOTO. | ||
| + | |||
| + | When an unconditional branch is used to point back to an earlier instruction, it can accomplish repetition in process. In 10 PRINT, the GOTO command is used to cause the program to loop, so that the PRINT command is repeated again and again. | ||
| + | |||
| + | The other type of branch is a conditional branch, a type of instruction that is critical to general-purpose computing. There are many different types of conditional branches in assembly, since there are many different types of conditions. BEQ is "branch if equal," for instance; when used after a comparison (CMP), the branch will be taken only if the compared values are equal. BMI, "branch if minus," checks to see if the last computation resulted in a negative value. In BASIC, using the IF ... THEN statement is the most straightforward way to accomplish a conditional branch, as this program demonstrates: | ||
<nowiki>10 INPUT A$ | <nowiki>10 INPUT A$ | ||
| − | 20 IF A$ = "1" THEN PRINT "FIRST" : END | + | 20 IF A$ = "1" THEN PRINT "ONE" : END |
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. "FIRST" 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 without PRINT "FIRST" and END being executed. "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. "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 and PRINT "ONE" and END are skipped. "SOMETHING ELSE" is printed instead. |
| − | While the conditional branch is an extremely important concept in computing, there is also a simpler type of branch, the unconditional branch. In assembly language, the mnemonic for this type of branch is JMP, meaning "jump." In BASIC, GOTO is the unconditional branch; the IF ... THEN ... statement can be used to control whether or not a particular GOTO statement is executed. | + | 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. |
| − | To make a program repeat everything it does without limit -- until the computer is turned off or the program otherwise is interrupted -- the program's last instruciton can be an unconditional "jump" back to the beginning, or, in BASIC terms, a GOTO that leads back to the first line of the program. This is how 10 PRINT works. | + | == This all needs reworking... === |
| + | |||
| + | ''Perhaps the single most important programming idea is that of a loop.'' | ||
| + | |||
| + | - ''BASIC: A Manual for BASIC, the elementary algebraic language designed for use with the Dartmouth Time Sharing System,'' 1 October 1964 | ||
=== GOTO and the Unbounded Loop === | === GOTO and the Unbounded Loop === | ||
