Programming Style and Techniques
The method you use for constructing your programs is just as important as the language you use to write them.
Consider the data
When you are faced with the task of writing a program, the first thing to consider is the data you are required to process. Make a list of the input data. What are the items and what are the possible values of each? If the items have a kind of structure or pattern, draw a diagram to illustrate it. Then do the same for the output data. Study your two diagrams and try to see if they fit together. If they do, you are well on the way to designing your program.
Next, write the specification that the user will use. This might be a written specification, a HELP file or both.
Last of all, write your program.
- You are required to write an interactive program that invites the user to play
Heads or tails
. The game can be played as long as the user likes. To end the game the user should replyQuitin answer to the questionHeads or tails?
The program is arranged so that the computer always wins.
Let's play a game! Type "Heads", "Tails",
or "Quit"
and press ENTER. This
means that there are four possible inputs: - HEADS
- TAILS
- QUIT
- None of these three.
- Sorry. It was TAILS. Hard luck!
- Sorry. It was HEADS. Hard luck!
- That's not a valid answer. Try again!
Now that you understand the specification, the input data and the output data, you are ready to write the program.
If you had started by writing down some instructions without considering the data, it would have taken you longer.