C4.1 Stage 4–6 Computing

Conditionals, robotics & spreadsheets

Create algorithms with loops, repetition, conditionals & nested instructions; educational robotics; geometric construction programs; spreadsheets.

An algorithm is a precise list of steps for getting something done. Once you can loop (repeat steps) and add conditionals (steps that only happen if something is true), you can write instructions that make decisions — the same skill that drives a floor robot, a geometry-construction program, or a spreadsheet.

What it means

A conditional is an instruction that runs only when a condition is met. In plain words it is an if statement: “if the light is red, stop.” The part after if is a yes/no question (a condition); the step after it only happens on “yes”. Add an else and you cover the “no” case too: “if it is raining, take an umbrella; else wear a hat.”

You already know loops (from C3.1): a loop repeats a block of steps. Conditionals make loops smart. Instead of “walk 10 steps”, a robot can “keep walking until you hit a wall” — it checks the condition each time round the loop and stops when the answer becomes “yes”.

Nested instructions means putting one block inside another — a conditional inside a loop, or a loop inside a conditional, like boxes inside boxes. The inner block runs only when the outer one lets it. Reading nesting carefully (what is inside what) is a big part of thinking like a programmer.

These same ideas appear in three real tools children meet at this stage:

  • Educational robotics — a floor robot (Bee-Bot, LEGO, or a Scratch sprite) follows an algorithm and reacts to sensors with conditionals.
  • Geometric construction programs — turtle graphics and GeoGebra build shapes from repeated, sometimes conditional, steps.
  • Spreadsheets — a cell can hold =IF(A1>10, "big", "small"), which is a conditional written as a formula, and formulas can be filled down (a loop over many rows).

Worked examples

1. If / else — sorting numbers

for each number in the list:
    if number is even:  put it in the LEFT pile
    else:               put it in the RIGHT pile

Feed in 4, 7, 2, 9. Left pile: 4, 2. Right pile: 7, 9.

2. Repeat-until — a robot finding a wall

repeat:
    move forward 1 step
until (wall ahead)

On a 3-square path the loop runs 3 times, checking “wall ahead?” each time; the 3rd check is “yes”, so it stops.

3. Spreadsheet conditional

A (score)B: =IF(A2>=50,"pass","fail")
72pass
41fail
50pass

Note >= means “greater than or equal to”, so 50 passes.

The generative-art connection

Conditionals are how a picture makes choices, and choices are what turn a plain grid into art. Take a turtle drawing a grid of dots and add one rule: if row + column is even, draw a filled dot; else leave it blank. That single conditional produces a checkerboard — the maths is literally visible in the pattern.

Change the condition and the art changes. “If the step number is a multiple of 3, turn red” scatters colour through a spiral in a rhythm you can see and count. In Scratch or Turtle Blocks you can build these in minutes: a repeat loop draws, an if inside it decides. In the internal dot-multiplier tool, one repeated rule fans a single dot into a symmetric starburst — the same “loop plus a rule” that grows a construction program into a whole design.

Common misconceptions

  • “The if step always runs.” No — it runs only when the condition is true. If the condition is false and there is no else, nothing happens and the program moves on.
  • “A loop and a conditional are the same thing.” A loop repeats; a conditional chooses. A repeat-until loop uses a conditional to decide when to stop, but the two ideas do different jobs.
  • “Nesting order doesn’t matter.” It does: a conditional inside a loop is checked every pass, while a loop inside a conditional runs only when that one condition is true. Swapping them can change the result completely.

What a learner should be able to solve

The benchmark for mastering this concept — the problems a child at this stage is expected to be able to work through.

  1. Read this algorithm and say what it draws on a `5 × 5` grid, where each square is at row `r` and column `c` (both counting `1..5`): ``` for each square: if r equals c: paint it BLUE else: leave it white ``` Which squares end up blue?

    Answer

    The blue squares are the ones where `r = c`: `(1,1), (2,2), (3,3), (4,4), (5,5)` — the diagonal line from top-left to bottom-right. A child masters this by correctly reading the condition and listing all 5 matching squares.

    Art hook Canvas 5×5 grid; loop over cells, fill blue when row===col. Add a slider that changes the condition to `r === 6-c` to flip the diagonal, or `r === c OR r === 6-c` to draw an X.

  2. A floor robot sits ON square 1 of a straight track and there is a wall right after square 6. It follows this rule: ``` repeat: move forward 1 square if the square number is even: beep until (wall ahead) ``` How many times does it move forward, and on which squares does it beep?

    Answer

    Starting on square 1, each forward move takes it to the next square: 1→2, 2→3, 3→4, 4→5, 5→6, and then the wall stops it. So it moves forward `5` times (landing on squares 2, 3, 4, 5, 6). The nested `if` fires on the even squares it lands on — `2, 4, 6` — so it beeps `3` times. Mastery = getting the move count right from the loop-until (no off-by-one) AND the beeps right from the nested `if`.

    Art hook Animate a dot hopping square to square on a number line; play a colour flash (or grow the dot) on even squares. The 'beep' becomes a visible pulse every 2nd step, and the dot halts at the wall.

  3. A spreadsheet has ages in column `A`. In column `B` you write the formula `=IF(A2>=13, "teen", "child")` and fill it down. Give the result in `B` for these ages: `8, 13, 12, 15`.

    Answer

    `8 → child`, `13 → teen` (because `>=` includes 13), `12 → child`, `15 → teen`. Mastery = applying the `>=` condition correctly, especially the boundary value 13.

    Art hook Colour-code a strip of dots by a threshold: dots with value `>= 13` glow one hue, the rest another. Drag the threshold and watch the colour split move — a live IF-statement you can see.

  4. Write your OWN short algorithm (using a loop and an `if`) that would colour every square of a `4 × 4` grid so that only the squares in an even-numbered row turn green. Use plain step-by-step words.

    Answer

    A correct answer loops over the squares and uses one conditional, e.g.: ``` for each square in the grid: if the row number is even: paint GREEN else: leave white ``` Rows 2 and 4 become green stripes. Mastery = producing a valid loop-plus-conditional that names the condition (row is even) and both outcomes.

    Art hook Horizontal green stripes on rows 2 and 4. Extend: let the learner type the condition (`row is even`, `column is even`, `row+col is even`) and instantly re-render — stripes, columns, or a checkerboard.

Training exercises

Practice problems, easier first, that build toward the bar above. Each doubles as a seed for an interactive artwork.

  1. An algorithm is just a precise list of steps. Put these steps for 'draw a square with a turtle' in the right order: (a) turn right `90°`, (b) repeat 4 times, (c) move forward 100. What is the correct order?

    Answer

    `(b) repeat 4 times`, then inside the loop `(c) move forward 100` and `(a) turn right 90°`. The loop body is 'move forward, turn' repeated 4 times, which closes the square.

    Art hook Turtle draws a square by repeating 'forward, turn 90'. Then let the turn angle be a slider: `120°` gives a triangle, `72°` a pentagon — a loop that draws any regular shape.

  2. True or false: an `if` step ALWAYS runs, no matter what.

    Answer

    False. An `if` step runs only when its condition is true. If the condition is false (and there is no `else`), that step is skipped and the program moves on.

    Art hook A row of dots where each dot 'checks a condition' (e.g. is my number prime?) and only lights up if true — the dark dots show the skipped cases.

  3. Trace this sorting rule with the list `6, 3, 8, 5`: ``` for each number: if number is greater than 5: put in the TALL pile else: put in the SHORT pile ``` What is in each pile?

    Answer

    TALL pile: `6, 8` (both `> 5`). SHORT pile: `3, 5` (note `5` is NOT greater than 5, so it is short).

    Art hook Draw each number as a vertical bar; taller bars slide right, shorter bars slide left — a live sort you can watch, threshold set at 5.

  4. A robot on a track follows: ``` repeat 5 times: move forward 1 if you are on a coloured tile: pick up a coin ``` The coloured tiles are on steps `2` and `4`. How many coins does the robot collect?

    Answer

    2 coins. The loop runs 5 times (steps 1–5); the nested `if` is true only on steps 2 and 4, so it picks up one coin each time = 2 coins total.

    Art hook Dot walks 5 steps along a line; coloured tiles flash and a small coin-dot pops up when the condition fires. Counter tallies coins collected.

  5. Spot the mistake. A child wants column `B` to say 'big' when the number in `A` is over 100. They wrote `=IF(A2>100, "small", "big")`. What went wrong, and how do you fix it?

    Answer

    The two outcomes are swapped: as written, numbers over 100 return 'small'. Fix: `=IF(A2>100, "big", "small")` — the true-result comes first.

    Art hook Threshold colouring where the 'true' and 'false' colours are swappable with a button — flipping them makes the palette mistake instantly visible.

  6. Nesting means one block inside another. In this algorithm, which step is INSIDE the `if`, and which is OUTSIDE it? ``` for each dot: if the dot is on an even step: paint it red move to the next dot ```

    Answer

    'Paint it red' is INSIDE the `if` (it runs only on even steps). 'Move to the next dot' is OUTSIDE the `if` but inside the loop (it runs every time, on every dot).

    Art hook Highlight the nesting visually: even-step dots turn red, but every dot advances — showing which action is conditional and which is unconditional.

  7. Change one rule to change the art. A grid uses: `if row + column is even, draw a filled dot; else leave blank` (a checkerboard). What NEW pattern do you get if you change the condition to `if row + column is a multiple of 3, draw a filled dot`?

    Answer

    You get diagonal stripes of dots: dots appear where `row + column` is `3, 6, 9, …`, forming parallel diagonal lines running across the grid instead of a checkerboard.

    Art hook Grid where the condition on `row+column` is a live control (`is even` / `multiple of 3` / `multiple of 4`) — each choice makes different diagonal rhythms; count the spacing between stripes.

  8. Design challenge: write a loop-and-conditional algorithm that makes a `6 × 6` grid where every square whose column number is a multiple of 3 turns purple. Which columns end up purple?

    Answer

    An algorithm like: ``` for each square: if column is a multiple of 3: paint PURPLE else: leave white ``` Columns `3` and `6` become purple vertical stripes.

    Art hook Vertical purple stripes at columns 3 and 6. Let the multiple be adjustable (every 2nd, 3rd, 4th column) to explore stripe spacing — a visual times-table.

  9. Reasoning: a repeat-until loop and a plain repeat loop are different. When would you choose `repeat until (wall ahead)` instead of `repeat 10 times` for a robot walking down a corridor?

    Answer

    Use `repeat until (wall ahead)` when you do not know how long the corridor is — the robot checks the condition and stops itself at the wall. Use `repeat 10 times` only when you already know the exact number of steps. The until-loop adapts; the counted loop is fixed.

    Art hook Two robots race down corridors of different lengths: the 'repeat 10' robot crashes or stops short, the 'repeat until wall' robot always stops perfectly — showing why the conditional stop is smarter.

  10. Open-ended: invent a spreadsheet IF-rule that would sort a column of test scores into THREE groups (say 'gold', 'silver', 'bronze') using nested IFs. Describe the rule in words.

    Answer

    Nest one IF inside another, e.g. `=IF(A2>=80,"gold", IF(A2>=50,"silver","bronze"))`. In words: if the score is at least 80 it's gold; otherwise if it's at least 50 it's silver; otherwise it's bronze. Any valid nesting with sensible non-overlapping bands is correct.

    Art hook Colour a strip of score-dots into three hue bands by two thresholds; drag either threshold and watch the gold/silver/bronze regions resize — a nested-IF made visual.

  11. Trace carefully — nesting order matters. Compare these two on a list of numbers `1, 2, 3, 4`. Algorithm A: `for each number: if number is even: print it`. Algorithm B: `if the list is long: for each number: print it`. If the list counts as 'long', do both print the same thing? Explain.

    Answer

    No. A prints only the even numbers: `2, 4`. B prints EVERY number (`1, 2, 3, 4`) once, because the whole loop is inside a single `if` that is true. Same pieces, different nesting, different output — the conditional-inside-loop checks every item, the loop-inside-conditional runs all-or-nothing.

    Art hook Side-by-side grids: version A lights only even dots each pass; version B lights all dots (or none) depending on the outer switch — a toggle demonstrates the nesting difference.