Читать книгу Beginning Programming All-in-One For Dummies - Wallace Wang - Страница 34
SPAGHETTI PROGRAMMING WITH THE GOTO COMMAND
ОглавлениеAlthough you can write spaghetti programs in any language, the BASIC programming language is most closely associated with spaghetti programming. Early versions of BASIC used a GOTO
command, which essentially told the computer to “go to” another part of the program.
The problem with the GOTO
command was that it could tell the computer to “go to” any part of the program. If you had a large program that consisted of several hundred (or several thousand) lines of code, the GOTO
command could tell the computer to jump from one part of the program to another in any order, as the following BASIC program shows:
10 GOTO 5020 PRINT "This line prints second"30 END40 GOTO 2050 PRINT "This line prints first"60 GOTO 40
Line 10 (the first line) tells the computer to “go to” line 50.
Line 50 tells the computer to print This line prints first
onscreen. After the computer follows this command, it automatically runs the next command below it, which is line 60.
Line 60 tells the computer to “go to” line 40.
Line 40 tells the computer to “go to” line 20.
Line 20 tells the computer to print This line prints second
onscreen. After the computer follows this command, it automatically follows the command on the next line, which is line 30.
Line 30 tells the computer this is the end of the program.
Even though this program consists of six lines, you can already see how the GOTO
command makes the computer jump around, so it's hard to understand how this program works. Now imagine this program multiplied by over several hundred lines of code, and you can see how spaghetti programming can make reading, understanding, and modifying even the simplest program much harder.