Remarks: Maze Walker
From Maze
(Difference between revisions)
| Line 7: | Line 7: | ||
Now 10-print must be modified to execute the PRINT statement 400 times. One way would be to write the following 400 line program: | Now 10-print must be modified to execute the PRINT statement 400 times. One way would be to write the following 400 line program: | ||
| + | <code> | ||
1 PRINT CHR$(205.5 + RND(1)); | 1 PRINT CHR$(205.5 + RND(1)); | ||
| − | |||
2 PRINT CHR$(205.5 + RND(1)); | 2 PRINT CHR$(205.5 + RND(1)); | ||
| − | |||
… | … | ||
| − | |||
400 PRINT CHR$(205.5 + RND(1)); | 400 PRINT CHR$(205.5 + RND(1)); | ||
| − | + | </code> | |
There are, however, significant problems with writing the program this way. For one thing, the program is long; the programmer must engage in the repetitive task of writing the same line over and over again (changing the line numbers). Computers are good at repetition; whenever a program involves tedious and repetitive steps, programmers look for ways to make the computer do the repetitive work. More significantly, this version of the program is very hard to change. To create a longer sequence of 600 symbols, 200 more lines must be added to the program. To create a shorter sequence of 100 symbols, 300 lines must be deleted from the program. To create a pattern of two different symbols, or to change the relative frequency with which the two symbols appear, the number 205.5 must be changed in every line. Clearly the best way to write this program is to write the PRINT line once and have the computer execute the line 400 times. The program must somehow count to 400, so that it can keep track of how many times it has executed the PRINT line and stop after 400 times. The following program does this, introducing the FOR NEXT construct in BASIC. | There are, however, significant problems with writing the program this way. For one thing, the program is long; the programmer must engage in the repetitive task of writing the same line over and over again (changing the line numbers). Computers are good at repetition; whenever a program involves tedious and repetitive steps, programmers look for ways to make the computer do the repetitive work. More significantly, this version of the program is very hard to change. To create a longer sequence of 600 symbols, 200 more lines must be added to the program. To create a shorter sequence of 100 symbols, 300 lines must be deleted from the program. To create a pattern of two different symbols, or to change the relative frequency with which the two symbols appear, the number 205.5 must be changed in every line. Clearly the best way to write this program is to write the PRINT line once and have the computer execute the line 400 times. The program must somehow count to 400, so that it can keep track of how many times it has executed the PRINT line and stop after 400 times. The following program does this, introducing the FOR NEXT construct in BASIC. | ||
