Regularity
From Maze
(Difference between revisions)
(→Iteration as a Generalized Process) | (→Iteration: Added section of the flow of control in imperitive programs) | ||
| Line 6: | Line 6: | ||
- The Original BASIC Manual, October 1964 | - The Original BASIC Manual, October 1964 | ||
| + | |||
| + | === The Flow of Control === | ||
| + | |||
| + | In any imperative programming language, commands are processed in a particular order that relates to the left-to-right, then top-to-bottom path that the Western reader's eyes stereotypically take along a page. If one types in two PRINT commands directly to the Commodore 64's BASIC interpreter, with a colon between them, like so: | ||
| + | |||
| + | PRINT "FIRST": PRINT "SECOND" | ||
| + | |||
| + | The result is | ||
| + | |||
| + | FIRST | ||
| + | SECOND | ||
| + | |||
| + | 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: | ||
| + | |||
| + | 10 PRINT "FIRST": PRINT "SECOND" | ||
| + | 20 PRINT "THIRD" | ||
| + | |||
| + | 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. | ||
| + | |||
| + | 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: | ||
| + | |||
| + | 10 INPUT A$ | ||
| + | 20 IF A$ = "1" THEN PRINT "FIRST" : END | ||
| + | 30 PRINT "SOMETHING ELSE" | ||
| + | |||
| + | 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. | ||
| + | |||
| + | 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. | ||
| + | |||
| + | 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. | ||
=== GOTO and the Unbounded Loop === | === GOTO and the Unbounded Loop === | ||
