Repetition, Selection and Variables
Programs are built from a few powerful ideas.
Repetition (Loops)
A loop repeats instructions, so you do not have to write them again and again.
To draw a square:
repeat 4 times
move forward 10
turn right 90 degrees
Four sides and four right-angle turns make a square.
- A count-controlled loop repeats a set number of times.
- A forever loop repeats until the program is stopped.
Selection (Decisions)
Selection lets a program choose what to do using a condition.
if score > 10
say "Well done!"
else
say "Keep trying!"
If the score is 8, the condition is false, so the program says "Keep trying!"
Variables
A variable is a named place that stores a value that can change while the program runs. Games use variables for score, lives and time.
set score to 0
change score by 1
change score by 1
After these instructions the score is 2.
Input and Output
- Input is information going into a program: a key press, a mouse click, a sensor or a microphone.
- Output is what the program produces: text on screen, a sound or a movement.
Putting It Together
In a simple game, a loop keeps the game running, selection checks "if the player touches the coin", a variable adds 1 to the score, and output plays a sound.
Events and Procedures
An event starts a piece of code, such as "when space key pressed". A procedure is a named group of instructions you can reuse.
Curriculum link: National Curriculum in England: Computing programmes of study (DfE) — Key Stage 2: use sequence, selection, and repetition in programs; work with variables and various forms of input and output