Branches:Branches consist of two or more groups of commands. At any given time, the computer may choose to follow one group of commands or another. Branches allow a program to make a decision based on a certain condition.For example, at the end of most video games, the program asks you, “Do you want to play again (Yes or No)?” If you choose Yes, the program lets you play the video game again. If you choose No, the program stops running, as shown in Figure 2-3.FIGURE 2-3: Branches let the computer choose which group of commands to run at any given time.A branch starts with a command that evaluates a condition (such as determining whether the user chose Yes or No). Then, based on this answer, whether it’s true or false, the branch chooses which group of commands to follow next.
Loops: Sometimes you may want the computer to run the same commands over and over again. For example, a program may ask the user for a password. If the user types an invalid password, the program displays an error message and asks the user to type the password again.If you wanted your program to ask the user for a password three times, you could write the same group of commands to ask for the password three times, but that would be wasteful. Not only would this force you to type the same commands multiple times, but if you wanted to modify these commands, you’d have to modify them in three different locations as well. Loops are basically a shortcut to writing one or more commands multiple times.A loop consists of two parts:The group of commands that the loop repeatsA command that defines how many times the loop should run
By combining sequences, branches, and loops, you can design any program and understand how the program works at each step.
Dividing a program into sequences, branches, and loops can help you isolate and organize groups of related commands into discrete “chunks” of code. That way, you can yank out a chunk of code, modify it, and plug it back in without affecting the rest of the program.
Top-down programming
For small programs, organizing a program into sequences, branches, and loops works well. But the larger your program gets, the harder it can be to view and understand the whole thing. So, a second feature of structured programming involves breaking a large program into smaller parts where each part performs one specific task. This is also known as top-down programming.
The idea behind top-down programming (as opposed to bottom-up programming) is that you design your program by identifying the main (top) task that you want your program to solve.
For example, if you wanted to write a program that could predict the next winning lottery numbers, that is a top design of your program. Of course, you can’t just tell a computer, “Pick the next winning lottery numbers.” You must divide this single (top) task into two or more smaller tasks.
One of these smaller tasks may be, “Identify the lottery numbers that tend to appear often.” A second task may be, “Pick the six numbers that have appeared most often and display those as the potential future winning numbers.”
The idea is that writing a large program may be tough, but writing a small program is easy. So, if you keep dividing the tasks of your program into smaller and smaller parts, eventually you can write a small, simple program that can solve that task. Then you can paste these small programs together like building blocks, and you’ll have a well-organized big program — theoretically.
Now if you need to modify part of the large program, just find the small program that needs changing, modify it, and plug it back into the larger program, and you’ve just updated the larger program.
Ideally, each small program should be small enough to fit on a single sheet of paper or a single screen. This makes each small program easy to read, understand, and modify. When you divide a large program into smaller programs, each small program is a subprogram.
If you divide a program into multiple subprograms, you have two options for where to store your subprograms:
Store all your subprograms in a single file. This option is fine for small programs, but after you start dividing a program into multiple subprograms, trying to cram all your subprograms into a single file is like trying to cram your entire wardrobe into your sock drawer. It’s possible, but it makes finding anything later that much more difficult.
Store subprograms in separate files, as shown in Figure 2-4. Storing subprograms in separate files offers three huge advantages:The fewer subprograms crammed into a single file, the easier it is to find and modify any of them.If you store subprograms in a separate file, you can copy that file (and any subprograms stored in that file) and then plug it into another program. In that way, you can create a library of useful subprograms and reuse them later.By reusing subprograms that you’ve tested already to make sure they work properly, you can write more complicated programs in less time, simply because you’re reusing subprograms and not writing everything from scratch.
FIGURE 2-4: You can store subprograms in one big file or in separate files.
STRUCTURED PROGRAMMING AND PASCAL
You can use structured programming techniques with any programming language, including machine language or assembly language (see Book 1, Chapter 1). However, the one language most closely associated with structured programming is Pascal.
Unlike other languages that later adopted structured programming, Pascal was designed to encourage (force) programmers to use structured programming from the start. A typical Pascal program may look like this:
Program Print2Lines;Begin Writeln ('This line prints first'); Writeln ('This line prints second');End.
Without knowing anything about the Pascal language, you can immediately make sense out of what it does.
First, it prints the line, This line prints first.
Next, it prints the second line, This line prints second.
Unlike the BASIC example that allows spaghetti programming (see “Spaghetti programming with the GOTO command,” earlier in this chapter), Pascal forces programmers to structure programs using sequences, branches, and loops. As a result, Pascal helps programmers create well-organized programs that are much easier to read and understand.
Event-Driven Programming
In the early days, using a program was fairly simple. After you typed the command to run a particular program, that program might ask a question such as
What is your name?
At this point, you had no choice but to type a name, such as Charlie Smith. After you typed in your name, the program might respond with
Hello, Charlie Smith. What month were you born?
The moment you typed in a month, such as April, the program might respond:
What day were you born?
And so on. If you wanted to type your day of birth before your month of birth, you couldn't because the program controlled your options.
Not surprisingly, using a computer like this was frustrating