Computer programs
We use computerA device that processes information by following a set of rules called a program. and computer-controlled devices in all areas of our lives.
They are extremely fast, accurate and brilliant at organising large amounts of dataInformation collected in the form of numbers.. 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 programA sequence of instructions written in a coding language that a computer can understand..
We write instructions that make up a computer program using a coding languageA language that may contain numbers, symbols and text than can be understood by a computer. that computers understand.
We need to organise our instructions so the computer can carry them out efficiently.
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:
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.
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.
Asking a question
Let's think about how to solve this problem with an algorithmA precise set of ordered rules or instructions that can be followed by a human or a computer to achieve a task..
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 conditional loopA repeat loop that contains a condition that must be met before a repeat loop will stop..
This is what it looks like as an algorithm flowchart.
Watch: Repetition
Learn more about repetition with this video from 91热爆 Teach.
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 selectionInstructions in a computer program where the program action depends on the outcome of a choice..
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!
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:
Programming a computer to make a selection
computer programmerSomeone that creates programs for a computer. 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:
Real life selection
Watch: Selection in computer programming
Find out more about selection in computer programming with this video from 91热爆 Teach.
Activities
Computing - Dance Mat Typing. gameComputing - Dance Mat Typing
Build and test your computing skills with different levels of touch type challenges
More on Computer science
Find out more by working through a topic
- count17 of 24
- count18 of 24
- count19 of 24
- count20 of 24