Lesson 5 of 5 · Changi Airport

Loops, If-Else & Functions: a 45-minute coding lesson

A free 45-minute lesson combining loops, conditions and functions for ages 9–12. Students sort luggage onto the right belts at Changi Airport.

  • 45 minutes
  • Pairs, whole class
  • One device per pair, plus a projector
Open level 1

Learning objectives

By the end of the lesson, students can:

  • Combine sequencing, loops, conditions and functions in one program.
  • Use a condition to handle input that is not known in advance.
  • Organise a longer program with a function.

Key words

Algorithm:
A step-by-step plan for solving a problem.
Input:
Information a program receives, like the colour of the next bag.
Decompose:
Break a big problem into smaller ones.

You'll need

  • One laptop, Chromebook or iPad per pair, open to dotcodecampus.com/maze/changi/1. No accounts or installs.
  • A projector or screen for the demo.
  • Optional: the printable unplugged grid game for the warm-up, and certificates to finish.

Lesson flow

  1. Minutes 0–8

    Unplugged warm-up

    • Hand out red and blue paper "bags" in a random order. One student sorts them while the class calls out the rule.
    • Write the algorithm together: pick up a bag; if it is red, put it left; else, put it right; repeat.
    • Ask: which part is a loop, which part is a condition, and what could be a function?
  2. Minutes 8–15

    Demo on the projector

    • Open Changi Airport level 3 (dotcodecampus.com/maze/changi/3) and build the sorting loop with the class.
    • Show how the same program works whatever order the bags come in.
    • Show the Python and identify the loop, the if / else and the function.
  3. Minutes 15–35

    Paired play: levels 1–6

    • Pairs work through levels 1–6, swapping roles at each level.
    • Encourage pairs to decompose: get one bag right first, then add the loop.
    • Level 5 uses "repeat until finished" because nobody knows how many bags are coming.
    • Students who finish every world can print a certificate at dotcodecampus.com/maze/certificate.
  4. Minutes 35–42

    Class discussion

    • Which of the four ideas (sequence, loop, condition, function) did your program use? Point to each one.
    • How does a real airport know where each bag should go? (Barcode tags and scanners: conditions!)
    • What would you like to build next with these four ideas?
  5. Minutes 42–45

    Exit ticket

    • Label each line of your level 5 Python as a sequence, loop, condition or function.

Early finishers: Invent a third belt colour. Write the sort_bag function on paper with if / elif / else.

Answer key

One three-star solution per level, in Python. Other solutions with the same number of blocks earn three stars too. Jump links take the class straight to a level.

Level 1: Check-in

4 blocks

dotcodecampus.com/maze/changi/1

move_up()
pick_up()
move_left()
drop()

Level 2: Holiday Rush

6 blocks

dotcodecampus.com/maze/changi/2

move_up()
for i in range(4):
    pick_up()
    move_left()
    drop()
    move_right()

Level 3: Red or Blue?

10 blocks

dotcodecampus.com/maze/changi/3

move_up()
for i in range(4):
    pick_up()
    if bag_is_red():
        move_left()
        drop()
        move_right()
    else:
        move_right()
        drop()
        move_left()

Level 4: Long Belts

14 blocks

dotcodecampus.com/maze/changi/4

move_up()
for i in range(3):
    pick_up()
    if bag_is_red():
        for j in range(3):
            move_left()
        drop()
        for j in range(3):
            move_right()
    else:
        for j in range(3):
            move_right()
        drop()
        for j in range(3):
            move_left()

Level 5: sort_bag()

15 blocks

dotcodecampus.com/maze/changi/5

def sort_bag():
    pick_up()
    if bag_is_red():
        for i in range(3):
            move_left()
        drop()
        for i in range(3):
            move_right()
    else:
        for i in range(3):
            move_right()
        drop()
        for i in range(3):
            move_left()

move_up()
while not finished():
    sort_bag()

Level 6: Two Carousels

19 blocks

dotcodecampus.com/maze/changi/6

def sort_bag():
    pick_up()
    if bag_is_red():
        for i in range(3):
            move_left()
        drop()
        for i in range(3):
            move_right()
    else:
        for i in range(3):
            move_right()
        drop()
        for i in range(3):
            move_left()

move_up()
for i in range(2):
    sort_bag()
move_down()
move_down()
for i in range(2):
    sort_bag()

Bonus: Lost & Found

15 blocks

dotcodecampus.com/maze/changi/bonus

def sort_bag():
    pick_up()
    if bag_is_red():
        for i in range(3):
            move_up()
        drop()
        for i in range(3):
            move_down()
    else:
        for i in range(3):
            move_down()
        drop()
        for i in range(3):
            move_up()

move_right()
while not finished():
    sort_bag()