Datasheet

14
Part I: Let’s Get Started
3. {
4. Move wrench to lug nut.
5. Turn wrench counterclockwise five times.
6. }
7. Move wrench to toolbox.
8. Release wrench.
The program starts with Step 1 just as before and grabs a wrench. In the
second step, however, before the program waves the wrench uselessly
around an empty bolt, it checks to see if a lug nut is present. If so, flow con-
tinues on with Steps 3, 4, and 5 as before. If not, however, program flow skips
these unnecessary steps and goes straight on to Step 7 to return the wrench
to the toolbox.
In computerese, you say that the program executes the logical expression
“is lug nut present?” This expression returns either a true (yes, the lug nut is
present) or a false (no, there is no lug nut there).
What I call steps, a programming language would normally call a statement.
An expression is a type of statement that returns a value, such as 1 + 2 is an
expression. A logical expression is an expression that returns a true or false
value, such as “is the author of this book handsome?” is true.
The braces in Tire Changing Language are necessary to tell the program which
steps are to be skipped if the condition is not true. Steps 4 and 5 are executed
only if the condition is true.
I realize that there’s no need to grab a wrench if there’s no lug to remove, but
work with me here.
This improved program still has a problem: How do you know that 5 turns
of the wrench will be sufficient to remove the lug nut? It most certainly will
not be for most of the tires with which I am familiar. You could increase the
number of turns to something that seems likely to be more than enough, say
25 turns. If the lug nut comes loose after the twentieth turn, for example, the
wrench will turn an extra 5 times. This is a harmless but wasteful solution.
A better approach is to add some type of “loop and test” statement to the
Tire Changing Language:
1. Grab wrench.
2. If lug nut is present
05_617977-ch01.indd 1405_617977-ch01.indd 14 7/6/10 11:38 PM7/6/10 11:38 PM