18. COMPUTERS. How are computer programs constructed?

 

We write a computer program because we want a computer or computing device to complete a task. This task might be part of a larger project, or it might be a way to solve a problem.

 

Computer programs and stories

When we write a story, we know it must have certain structures to make it work for the reader.

The story will have events, or things that happen in a particular order, or sequence. We call this the plot, and the sequence is usually the beginning, followed by the middle and finally the end.

The story will also usually include descriptions of the characters and the story setting.

Let’s look at a real-world example

In a drinks factory, the machines are all controlled by computers. The machines:

  1. Move the empty bottles automatically around the different sections
  2. Fill up the bottle
  3. THEN put the lids on

Can you describe what would happen if they got the computer program sequence wrong and put the lids on first?

Repetition – let’s do it again

Repeating something, or doing the same thing more than once, can be a good way to learn or remember something new.

Imagine you were going to show an alien how to help tidy up your room. You would have to give them careful instructions.

You could say:

  1. Walk up to a toy on the floor
  2. Pick up the toy

Put the toy in the toy cupboard

But there are 20 things to tidy up – you must have been very untidy lately.

You could just repeat the same  20 times, but it would take a long time to tell the alien the same instructions over and over again.

Nearly as long as it would take to tidy your room!

If the alien was a robot you could use a  instruction like this:

Repeat 20 times:

  1. Walk up to a toy on the floor
  2. Pick up the toy
  3. Put the toy in the toy cupboard

The repeat command turns the instruction list into a repeat loop, where we go round and round the loop 20 times.

Loading

error: Content is protected !!