Sign in

← All revision topics

English variety

Conditionals and variables in block-based programming

Machine translation

Around age 10 · Usually taught in ⁨Key Stage 2⁩ · Free

Learn

  1. A program sometimes has to make a decision. A conditional does that. It says: if something is true, do this. If not, do something else.

    if points > 5 then
    say “You win”
    else
    say “Keep going”
    A conditional: whatever comes after “if” is the condition
  2. The condition is either true or false. If it is true, only the “then” part is done. If it is false, only the “else” part is done. The two parts are never both done.

    A variable is a box with a name. It holds a value, and the value can change. “Set points to 0” stores a 0. “Add 2 to points” adds 2 to the value that was there.

    StepBlockpoints
    1set points to 00
    2add 2 to points2
    3add 2 to points4
    The value of points after each block
  3. “Set” and “add” are not the same. “Set” wipes the old value and stores a new one. “Add” keeps the old value and adds something to it.

    If you copy one variable into another, the copy holds the value at that moment. If the original changes afterwards, the copy does not change.

    StepBlockab
    1set a to 55 
    2set b to a55
    3add 2 to a75
    b holds a copy of the value of a, but it does not change when a does
  4. To follow a program, write the value of the variable after each block in a table. You can combine loops, conditionals and variables.

    set n to 0
    set points to 0
    repeat 4 times
    add 1 to n
    if n is even then
    add 5 to points
    A loop with a conditional inside: points ends up as 10

Worked examples

Example 1

Julie has 4 points. The program says: if points > 5 then say “You win”; else say “Keep going”. What does it say?

if points > 5 then
say “You win”
else
say “Keep going”
  1. Look at the condition: points > 5.
  2. With 4 points, 4 > 5 is false.
  3. Since it is false, the “else” part is done: it says “Keep going”.

It says “Keep going”.

Watch out! Here many people say both things, “You win” and “Keep going”.

Common mistake

Example 2

Tony follows this program: set coins to 5, add 3 to coins, subtract 2 from coins. How many coins does he have at the end?

StepBlockcoins
1set coins to 55
2add 3 to coins8
3subtract 2 from coins?
  1. Set coins to 5: coins is worth 5.
  2. Add 3: 5 and 3 make 8.
  3. Subtract 2: 8 minus 2 is 6.

At the end he has 6 coins.

Watch out! Here many people forget the 5 from before and work out 3 minus 2.

Common mistake

Example 3

Gus writes: set a to 5, set b to a, add 2 to a. What are a and b worth?

StepBlockab
1set a to 55 
2set b to a55
3add 2 to a75
  1. a holds 5.
  2. b holds a copy of the value of a: 5.
  3. Adding 2 changes only a: a is worth 7. b is still worth 5.

a is worth 7 and b is worth 5.

Watch out! Here many people think b changes when a does and say b is worth 7.

Common mistake

A challenge to think about

Invent a points game. Start at 0, add points each round and use a conditional that says “You win” when you reach a target. Write the program with blocks and follow it in a table with someone.

Practise

Rounds of 10 questions. Choose a level:

Your grade in this topic:No grade yetDo a round at the Basic level to get a grade.

Next topic: Flowcharts and algorithms written in natural language →