Loops and selection
Computer Science · Key Stage 2 · The School
Loops: say it once
Writing "forward, turn, forward, turn, forward, turn, forward, turn" is tiring and error-prone. A LOOP says it once: repeat 4 [forward, turn]. Loops are how programs handle repetition — from drawing shapes to checking every player in a game, the computer happily repeats a million times.
Selection: if this, then that
Programs make choices with IF: IF it is raining THEN take an umbrella ELSE wear a cap. In a game: IF the player touches the coin THEN add a point. The condition (raining? touching?) is a yes/no question; the program takes one path or the other, never both.
Combining them
Real programs weave the two. A game loop: REPEAT forever [IF the player presses jump THEN jump; IF the player touches a spike THEN lose a life]. Almost everything on a screen — games, apps, robots — is loops around selections around simple steps.
A program says: `if temperature > 25 then turn on fan`. It runs when the program reaches it, finds 22, and moves on forever — the fan never comes on, however hot the room gets. Wrap it in a loop: `repeat forever { if temperature > 25 then turn on fan else turn off fan }`. Now it is checked continuously. Almost every 'it only worked once' bug is an `if` that needed a loop around it.