15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Planning Ahead with Structured Programming 33<br />

Spaghetti <strong>programming</strong> with the GOTO Command<br />

Although you can write spaghetti programs in<br />

any language, the BASIC <strong>programming</strong> language<br />

is most closely associated with<br />

spaghetti <strong>programming</strong>. Early versions of<br />

BASIC used a GOTO command, which essentially<br />

told the computer to “go to” another part<br />

of the program.<br />

The problem with the GOTO command was that<br />

it could tell the computer to “go to” any part of<br />

the program. If you had a large program that<br />

consisted of several hundred (or several thousand)<br />

lines of code, the GOTO command could<br />

tell the computer to jump from one part of the<br />

program to another in any order, as the following<br />

BASIC program shows:<br />

10 GOTO 50<br />

20 PRINT “This line prints<br />

second.”<br />

30 END<br />

40 GOTO 20<br />

50 PRINT “This prints first.”<br />

60 GOTO 40<br />

Line 10 (the first line) tells the computer to “go<br />

to” line 50.<br />

Line 50 tells the computer to print, This<br />

prints first. on-screen. After the computer<br />

follows this command, it automatically<br />

runs the next command below it, which is line 60.<br />

Line 60 tells the computer to “go to” line 40.<br />

Line 40 tells the computer to “go to” line 20.<br />

Line 20 tells the computer to print, This line<br />

prints second. After the computer follows<br />

this command, it automatically follows the<br />

command on the next line, which is line 30.<br />

Line 30 tells the computer this is the end of the<br />

program.<br />

Even though this program consists of six lines,<br />

you can already see how the GOTO command<br />

makes the computer jump around, so it’s hard<br />

to understand how this program works. Now<br />

imagine this program multiplied by over several<br />

hundred lines of code, and you can see how<br />

spaghetti <strong>programming</strong> can make reading,<br />

understanding, and modifying even the simplest<br />

program much harder.<br />

Book I<br />

Chapter 2<br />

Different Methods<br />

<strong>for</strong> Writing<br />

Programs<br />

The three parts of structured <strong>programming</strong><br />

To keep programs organized, structured <strong>programming</strong> teaches programmers<br />

that any program can be divided into three distinct parts:<br />

✦ Sequences<br />

✦ Branches<br />

✦ Loops<br />

Sequences<br />

Sequences are simply groups of commands that the computer follows one<br />

after another. Most simple programs just consist of a list of commands that<br />

the computer follows from start to finish, as shown in Figure 2-2.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!