Simple Programming Tasks

Putting Things in Order

Here are a few simple sentences. See if you can put them into an order that makes reasonable sense.


Drive to the end of the road.
Put the key in the ignition.
Unlock the car door.
Start the engine.
Leave the house.
Remove the steering lock.

Modula 3 programs consist of a sequence of steps:


do this;
do that;
do the other;

In the exercise above, is it necessary to `Unlock the car door' before you can `Start the engine'? Sometimes the order in which things are done matters. Is this true of the exercise above - is there only one way to put the sentences together to make sense? Now try putting the following sentences in order:


Cycle to work.
Get out of bed.
Eat breakfast.
Get dressed.
Wash in the bathroom.
Wash the breakfast dishes.

Is it possible to arrange these sentences in different orders and still have them make sense? Sometimes there is only one order in which things may be done and sometimes we can achieve the same task with different orders.

Here are some Modula 3 exercises along these lines. Place the following statements in order so that they will read two integers and print their sum.


Put.Text("Please type the second integer: ");
Get.Int(second);
Put.Int(Sum);
Sum := first+second;
Get.Int(first);
Put.Text("Please type the first integer: ");

Is there only one order for these statements to make sense (think carefully about this)?

Sometimes different orders are possible, but the outcomes are quite different. Consider the consequences of the following two orders:


Sell old car.
Pay money into the bank.
Write cheque to buy new car.

and


Write cheque to buy new car.
Sell old car.
Pay money into the bank.

Although both orders are possible, the result of the second might well be either a bounced cheque or a large overdraft! The order matters when one action is dependent upon the outcome of an earlier action or, alternatively, when one action has an effect upon the outcome of a later action. In the Modula 3 example above, the action:


Put.Int(Sum);

depends upon first having done:


Sum := first+second;

This, in turn, depends upon the two Get.Int statements.

Summary

Computer programming in Modula 3 consists of putting a series of steps into a logical order. Sometimes the order of those steps will be important, because some steps depend upon others. Putting statements in the wrong order is one reason why programs often do not work as they should.

To
Next Page

To Overview