All free lessons / Computing / Programming I: sequence and repetition

Computing · FSCE

Programming I: sequence and repetition

A free preview from the LearnAhead Learning Library — the same lesson registered students learn from.

In this lesson: Write and trace short programs built from instructions in sequence, and use repetition (loops) to replace repeated commands, predicting exactly where a sprite or robot will end up.

The idea

A program is a set of instructions a computer follows exactly, in order. That order is called the sequence, and it matters completely: 'pour the water, then add the teabag' is not the same program as 'add the teabag, then pour the water' — well, it nearly is, but 'put on socks, then shoes' reversed is a disaster. Computers never guess what you meant; they do what you said, so programmers must say it precisely and in the right order.

Real programs would be endless if every step had to be written out. Repetition solves this: a loop repeats a block of instructions a set number of times. Drawing a square needs 'forward, turn 90 degrees' four times — written once inside a repeat-4 loop instead of four times in a row. Loops also reduce mistakes: change the side length once, and all four sides change together.

FSCE computing questions hand you a short program — blocks, arrows or simple code — and ask you to trace it: follow the instructions one by one, keep track of position and direction, and state the final result. The winning habit is to work on paper, step by step, never in your head in one leap.

Worked example

A robot starts facing north. It follows this program: forward 2, turn right, forward 2, turn right, forward 2. Which statement is true at the end? A: It faces north, 6 squares from the start. B: It faces south, and has drawn three sides of a square. C: It faces east, back at the start. D: It faces west, 2 squares from the start.

  1. Trace it: forward 2 facing north; turn right (now east), forward 2; turn right (now south), forward 2. The robot has walked three sides of a square and faces south.
  2. A fails (facing is south, not north); C fails (it is not back at the start — one side is missing); D fails on both direction and distance.

Answer: B — after two right turns the robot faces south, having walked three sides of a square.

Loops: saying it once instead of four times

A repeat loop runs its block a fixed number of times, then the program moves on. 'Repeat 4: forward 10, turn 90' draws a square. 'Repeat 3' of the same block draws three sides of a triangle only if the turn is 120 degrees — the turn angle and the repeat count must match the shape: count times angle must total 360 degrees for a closed shape. Programmers check loops by counting: how many times does each instruction inside actually run?

Worked example

A program says: repeat 6 (forward 5, turn 60). What shape will the sprite draw, and how far does it travel in total? A: A square, travelling 20. B: A hexagon, travelling 30. C: A triangle, travelling 15. D: A circle, travelling 60.

  1. Six repeats with a 60-degree turn: 6 x 60 = 360 degrees, so the shape closes with six sides — a hexagon.
  2. Total distance is 6 repeats x 5 = 30. A miscounts the sides; C uses three repeats; D mistakes a polygon for a circle and miscalculates the distance.

Answer: B — a hexagon, with total travel of 30 units.

When the sequence is wrong

The commonest beginner bug is a correct set of instructions in the wrong order — 'turn, then move' instead of 'move, then turn'. Each instruction can be right while the program is wrong. When tracing, treat every line as final: the computer cannot know you 'meant' the other order. Programmers prevent sequence bugs by planning the algorithm first (often as a flowchart) and testing with a dry run before running the real thing.

Worked example

A program to draw the letter L says: forward 4, turn right, forward 2 — but the sprite was supposed to draw a tall L with a short foot to the right, starting by going up. The sprite instead goes right first, then down. What is the most likely bug? A: The loop count is wrong. B: The first instruction was written as 'forward' along the wrong starting direction — the sprite started facing east instead of north. C: The computer is broken. D: The answer needs a bigger number.

  1. The moves happen, but rotated: the whole drawing is turned 90 degrees, which points to the starting direction being wrong, not the steps — B.
  2. There is no loop (A); computers follow instructions faithfully (C); numbers were not the issue (D).

Answer: B — the starting direction was wrong, so the correct sequence drew a rotated L.

Watch out

Quick check

Try these on your own first — then reveal each answer.

1. What does 'sequence' mean in programming?

2. A loop says repeat 4 (forward 3, turn 90). How many times does 'forward 3' run, and what shape appears?

3. Why do programmers use loops instead of writing the same lines many times?

Was this lesson helpful?

Practise this concept

Reading is the first step — the concept sticks when your child answers questions on it. Registered students practise graded questions for this lesson, with instant marking and worked explanations, and the lesson is marked complete in their progress.