Get jack
Take off hubcap
Jack up car
Take off lug nuts
Take off damaged tire
Get spare tire from trunk
Put on spare tire
Put on lug nuts
Jack down car
Put on hubcap
Put damaged tire in trunk
An algorithm is a well-defined procedure which terminates in a finite number of steps. Well-defined means that there is no ambiguity. Each step in the procedure can clearly be understood to tell what is to be done. An example of not well-defined procedure is:
The above is not well-defined, because it does not tell you what to do if there is no coin in your pocket in step 1. Also, what if you have no pocket? We modify it to make it well-defined by:
1. If there is a coin in your pocket, then put it on the table.
Then if you have no pocket or if you have a pocket and no coin in there, there is nothing to do. If both the conditions are satisfied, then take it out of the pocket and put it on the table (assuming that the table exists).
So the above, should be changed to:
If there is a hubcap, take if off.
If there is a spare, get it from the trunk.
An example of an algorithm which does not terminate is the following:
1. If there is a coin in your pocket, then put it on the table.
2. Go to step 1.
In this example, it does not tell you what to do if you no longer have any coins or you have no pocket. We change it to:
1. While there is a coin in your pocket
a. Put the coin on the table.
b. Go back to step 1.
Since there is more than one lug nut to take off the wheel, we change it to:
1. While there is a lug nut to take off
a. Take off the lug nut
b. Go back to step 1.
The reason we change to a while statement, is that we don’t know how many lug nuts there are. It depends on the car make and model. For example, some have 4, others 6.
Notice that the algorithm has some undefined (but hopefully understood) terms (which we have put in boldface):
Get jack
Take off hubcap
Jack up car
Take off lug nuts
Take off damaged tire
Get spare tire from trunk
Put on spare tire
Put on lug nuts
Jack down car
Put on hubcap
Put damaged tire in trunk
(We could have included the trunk as well.) These are called primitives of the algorithm. They are parts of a car in this case. We describe them as, for example, the jack is the devise to lift part of the car off the ground, the hubcap is the object which covers the lug nuts, etc on the wheel.
We draw the flowchart of the algorithm as:
Take off hubcap Get jack
F
T
Jack up car
F
T
F
Get Help
T
Get spare tire
from trunk Put on spare
tire
F
T
F
T
Put hub cap on
T
Exit
In this algorithm, we show the two basic types of control structures that exist in programming languages: the conditional (if…then…else) and the loop (while or for). The if…then…else is illustrated by the following parts of the flowchart:
T F
This is illustrated above as an if…then with the “Is there a hubcap?”, and the if…then…else with the “Is there a spare tire?”
The while condition is illustrated by the following part of the flowchart:
T
The while loop is illustrated above with the “Is there a lug nut to be removed?”
The if…then…else construct asks if something is true or not, and depending on the result does either one of two possible things. It is possible that only one thing is possible to be done (the if…then) if the condition is satisfied.
The while condition or loop, asks if something is true, and if true, does something and then asks again if the condition is still true. It continues to do the loop until the condition is no longer satisfied. We say it is looping or repeating. One must be careful when using the looping construct to not have what is called an infinite loop. This happens when the condition that one tests for in the while condition is never satisfied so that one continues to do the body of the loop indefinitely.
The if…then…else and while condition are the primitives we use in high level programming languages. They can be translated into a machine language which only use branch or branch on negative statements (as well as load, store, add, etc). The branch and branch on negative are the primitives of the machine language.