34 Planning Ahead with Structured Programming Ask for the user’s income Figure 2-2: Sequences consist of groups of commands that the computer follows one after another. Multiply user’s income by tax rate Display taxes owed Display threatening warning 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. Play video game Figure 2-3: Branches let the computer choose which group of commands to run at any given time. Yes Do you want to play again? No Quit Program
Planning Ahead with Structured Programming 35 A branch starts with a command that evaluates a condition (such as determining whether the user chose Yes or No) and then based on this answer, chooses which group of commands to follow next. Book I Chapter 2 Loops Sometimes you may want the computer to run the same commands over and over again. For example, a program might 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. Different Methods for Writing Programs 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’d 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 repeats ✦ A 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.