For Dummies Beginning Programming with C++ 978-0-470-61797-7 User Manual

Product codes
978-0-470-61797-7
Page of 12
13
 Chapter 1: What Is a Program?
The program
Now it’s time to convert the algorithm, written in everyday English, into a 
program written in Tire Changing Language. Take the phrase, “Remove the 
lug nuts.” Actually, quite a bit is left unstated in that sentence. The word 
“remove” is not in the processor’s vocabulary. In addition, no mention is 
made of the wrench at all.
The following steps implement the phrase “Remove a lug nut” using only the 
verbs and nouns contained in Tire Changing Language:
 1. 
Grab 
wrench.
 
2.  Move wrench to lug nut.
 
3.  Turn wrench counterclockwise five times.
 
4.  Move wrench to toolbox.
 5. 
Release 
wrench.
I didn’t explain the syntax of Tire Changing Language. For example, the 
fact that every command starts with a single verb or that the verb “grab” 
requires a single noun as its object and that “turn” requires a noun, a direction, 
and a count of the number of turns to make. Even so, the program snippet 
should be easy enough to read (remember that this is not a book about Tire 
Changing Language). 
 
You can skate by on Tire Changing Language, but you will have to learn the 
grammar of each C++ command.
The program begins at Step 1 and continues through each step in turn until 
reaching Step 5. In programming terminology, we say that the program flows 
from Step 1 through Step 5. Of course, the program’s not going anywhere — 
the processor is doing all the work, but the term “program flow” is a common 
convention.
Even a cursory examination of this program reveals a problem: What if there 
is no lug nut? I suppose it’s harmless to spin the wrench around a bolt with 
no nut on it, but doing so wastes time and isn’t my idea of a good solution. 
The Tire Changing Language needs a branching capability that allows the 
program to take one path or another depending upon external conditions. We 
need an IF statement like the following:
 1. 
Grab 
wrench.
 
2.  If lug nut is present
05_617977-ch01.indd   13
05_617977-ch01.indd   13
7/6/10   11:38 PM
7/6/10   11:38 PM