Anda di halaman 1dari 2

Student Notes

Algorithms
An algorithm is a sequence of steps designed to perform a particular task.

A computer is not intelligent enough to do things by itself - it is only capable


of doing what we tell it. The idea of an algorithm is to describe exactly how to
do something so that any dumb person (or computer!) can do it without
understanding how the person who wrote it was thinking.
Consider these steps for making a cup of tea:
1. Put teabag in cup
2. Fill kettle
3. Boil kettle
4. Pour water into cup
5. Add milk
6. Stir
7. Drink
Say we wanted to tell this to a robot so that it can make tea for us. Is it
precise enough? Do you think that the robot will be able to understand it? It
probably isn't precise enough. For example, we haven't said where we get
the cup or teabag from, or that we have to put the milk back in the fridge
afterwards to prevent it going off.

SEQUENCE
We must refine the algorithm in simple steps that are carried out once each
and in order:
1. Get tea-making equipment:
a. Open cupboard
b. Get cup
c. Get teabag
d. Close cupboard
e. Open fridge
f. Get milk
g. Close fridge
2. Put teabag in cup
3. Fill kettle from tap:
a. Place kettle under tap
b. Turn on tap
c. Turn off tap
4. Boil kettle:
a. Plug kettle in
b. Turn kettle on
5. Pour water into cup

1/2 Aquilina K.
Student Notes

6. Add milk to cup


7. Replace milk:
a. Open fridge
b. Place milk in fridge
c. Close fridge
8. Stir
9. Drink
This is looking better, but it probably still isn't complete. There are things like
getting a spoon to stir with that we haven't covered. Also, what happens if
something goes wrong that we didn't anticipate? What if we're out of tea?
We also haven't specified how to tell that the kettle is full, or what to do if
there is a power cut and the kettle never boils.
The conclusion we are rapidly reaching here is that making tea is actually
quite a complicated task. In order to get a computer to make tea, we have to
explain the tea making process using an algorithm that only contains steps
that the computer understands.
Algorithms follow instructions given in a sequence like that in the tea making
example above.

DECISION MAKING
Algorithms generally have the ability to make simple choices as to what to
do:
if there are no teabags in the cupboard
then go out to the grocer and buy a packet
else put a teabag in the cup

REPEAT (LOOP)
Another common task of an Algorithm is to repeat a set of commands until
some condition is met:

Repeat the kettle is not full


wait a bit
until turn the tap off

So as we have seen an algorithm is made up of 3 main types of components;

Sequence - a group of steps is carried out once each and in order


Selection - this has 2 or more parts, but a choice has to be made and only
one of the parts is carried out
Repetition (Loop) - part of the algorithm is repeated, usually a fixed number
of times or until the condition is met.

2/2 Aquilina K.

Anda mungkin juga menyukai