Regularity
From Maze
(Difference between revisions)
(→GOTO and the Unbounded Loop) | (→The Flow of Control: Fixed up code formatting) | ||
| Line 11: | Line 11: | ||
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: | 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" | + | <nowiki>PRINT "FIRST": PRINT "SECOND"</nowiki> |
The result is | The result is | ||
| − | FIRST<br /> | + | <nowiki>FIRST |
| − | SECOND | + | 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: | 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"<br /> | + | <nowiki>10 PRINT "FIRST": PRINT "SECOND" |
| − | 20 PRINT "THIRD" | + | 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 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. | ||
| Line 29: | Line 29: | ||
In BASIC, the IF ... THEN ... statement is used to branch conditionally, so that different code is executed depending upon the value of an expression: | 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$<br /> | + | <nowiki>10 INPUT A$ |
| − | 20 IF A$ = "1" THEN PRINT "FIRST" : END<br /> | + | 20 IF A$ = "1" THEN PRINT "FIRST" : END |
| − | 30 PRINT "SOMETHING ELSE" | + | 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. "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. | ||
