91热爆

Computer programs

We use and computer-controlled devices in all areas of our lives.

They are extremely fast, accurate and brilliant at organising large amounts of . They can also perform many tasks at the same time.

Before they get to work, we must tell them what to do using a computer .

We write instructions that make up a computer program using a that computers understand.

We need to organise our instructions so the computer can carry them out efficiently.

Back to top

Repeat, repeat, repeat

One important way that we organise instructions in a program is with a REPEAT command.

Let's think about photocopying a document 30 times.

If we had to enter each copy command separately, we'd be at the photocopier all day.

We can tell the photocopier we want to print 30 copies by filling in the number in the photocopier settings.

This instructs the printing program to repeat the copying process 30 times.

The repeat instruction goes first. We call this kind of repeat command a fixed loop as the number of repeats is fixed.

This program might look like this:

Back to top

Repeating until something happens

Sometimes we don't know exactly how many times we want to repeat an action or task.

We might want to repeat it UNTIL something happens.

When we are washing up, we don't wipe the sponge across the plate a set number of times. We wipe the sponge UNTIL the plate is clean. We keep checking the plate for food and when we see it is clean, we stop wiping.

Back to top

Programming with REPEAT UNTIL

Using the REPEAT UNTIL command in a computer program means we are asking the program to keep on repeating an action until a specific thing happens.

Let's say we want a robot in a game to walk across the screen and stop at the edge.

The problem is that we don't know the size of the screen it might be walking across.

We can use the REPEAT UNTIL command to make it stop when it reaches the edge of the screen, whatever the screen size is.

But how will the program know when the robot reaches the edge? Now we have a problem to solve.

Back to top

Asking a question

Let's think about how to solve this problem with an .

We know the program will need to check if the robot has reached the edge of the screen. The program will have to ask this question.

Have you reached the edge of the screen yet?

  • If the answer is YES, then the robot will stop.
  • If the answer is NO, then the robot will carry on walking forward, while checking for the edge of the screen.

Because this type of repeat command asks the program to check the conditions it finds, we call it a .

This is what it looks like as an algorithm flowchart.

Back to top

Watch: Repetition

Learn more about repetition with this video from 91热爆 Teach.

Back to top

Selection - what should I do?

We can see from looking at the conditional loop example above that adding questions to a computer program can help it solve tricky problems.

We can use questions in computer programs to get a computer to make a choice or a .

This allows them to deal with different situations and problems, running different sections of code or even separate programs for different conditions.

The use of selection in computer programs is what makes them so flexible and good at dealing with changing situations like traffic, weather and everyday life!

Back to top

Making choices

You are getting ready for a day out in the park and wonder whether to take your umbrella. You need to make some choices, depending on the weather.

You take a quick look out of the window. If it is raining, then you will take an umbrella. If not, then you can leave it at home.

We make these kinds of choices all the time, checking conditions and making decisions based on what we find.

We can draw a flowchart to show how this works:

Back to top

Programming a computer to make a selection

call this way of organising instructions in a program selection.

The program is selecting an answer or action from several choices.

We often use program commands like IF,THEN and ELSE when we write programs that use selection.

  • ELSE is used in coding language to say 'otherwise' or 'if not'.

  • It is usually quicker to use IF and ELSE instead of YES and NO.

If we wanted to program a robot to go to the park, then you could write a program like this:

Back to top

Real life selection

Back to top

Watch: Selection in computer programming

Find out more about selection in computer programming with this video from 91热爆 Teach.

Back to top

Activities

Back to top

Computing - Dance Mat Typing. game

Build and test your computing skills with different levels of touch type challenges

Computing - Dance Mat Typing
Back to top

More on Computer science

Find out more by working through a topic