Skip to content
KALANBLON
Show contents

Chapter 2 of 4 · 15 min

Deciding and repeating

By the end of this chapter, you'll be able to

  • Write a decision with if… else
  • Write a repetition with while
  • Write a repetition with for each
  • Recognise a repetition that would never stop

Deciding: if… else

A condition is a question whose answer is true or false: "is it raining?", "is the age under 12?". An if… else instruction runs one block of instructions when the condition is true, and another when it is false.

if it_is_raining then
    take the umbrella
else
    leave the umbrella
end if

The lines shifted to the right (we say indented) form a block: they depend on the line above.

Repeating: while

A while loop repeats a block as long as its condition is true. It checks the condition before each round. Example: we fill a 10-litre bucket with a 3-litre cup.

We display 12. The loop made 4 rounds.

Step 12 of 12
Text description of the animation
  1. The bucket starts empty. Watch how many rounds the loop makes.
  2. Line 1: the bucket holds 0 litres.
  3. Line 2: the condition is true, we enter the loop.
  4. Line 3: we pour one cup, the bucket holds 3 litres. Back up to the condition.
  5. The condition is still true: another round.
  6. The bucket holds 6 litres.
  7. Still true: another round.
  8. The bucket holds 9 litres.
  9. 9 is still less than 10: one last round.
  10. The bucket holds 12 litres: it overflowed a little!
  11. The condition is false: we leave the loop.
  12. We display 12. The loop made 4 rounds.

Here is the same thing in Python, which you'll discover in detail later:

bucket = 0
while bucket < 10:
    bucket = bucket + 3
print(bucket)
12

Repeating: for each

When you know the items to process in advance, you use for each: "for each guest, set a plate". The loop stops on its own when there are no items left.

for each guest in the guest list
    set a plate
end for
guests = ["Ana", "Kwame", "Mei"]
for guest in guests:          # for each
    print("Plate for", guest)
Plate for Ana
Plate for Kwame
Plate for Mei

Your turn

Mini-lab: change the cupPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Worked exercise

Worked exercise: the ticket price

At the museum, a ticket costs €0 for children under 12, and €8 otherwise. Write an algorithm in pseudocode that starts from a variable age and displays the ticket price.

Hint 1
There is only one decision to make: is the age under 12?
Hint 2
Store the price in a variable price in each of the two cases, then display it after the end if.
Show the solution and common mistakes
age ← 15
if age < 12 then
    price ← 0
else
    price ← 8
end if
display price

With age ← 15, the algorithm displays 8.

Common mistakes:

  • Writing two separate "if"s (if age < 12 then if age > 12): a child aged exactly 12 would then have no price at all!
  • Forgetting the end if: you can no longer tell where the block ends.

Summary

  1. 1A condition is a question answered by true or false.
  2. 2if… else runs one block or the other depending on the condition.
  3. 3while repeats a block as long as its condition is true.
  4. 4for each repeats a block once for every item in a list.
  5. 5A loop must always end up making its condition false.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.