Conditionals and variables in block-based programming
Machine translation
Learn
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 thensay “You win”elsesay “Keep going”A conditional: whatever comes after “if” is the condition 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.
Step Block points 1 set points to 0 0 2 add 2 to points 2 3 add 2 to points 4 The value of points after each block “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.
Step Block a b 1 set a to 5 5 2 set b to a 5 5 3 add 2 to a 7 5 b holds a copy of the value of a, but it does not change when a does 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 0set points to 0repeat 4 timesadd 1 to nif n is even thenadd 5 to pointsA 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?
- Look at the condition: points > 5.
- With 4 points, 4 > 5 is false.
- 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”.
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?
| Step | Block | coins |
|---|---|---|
| 1 | set coins to 5 | 5 |
| 2 | add 3 to coins | 8 |
| 3 | subtract 2 from coins | ? |
- Set coins to 5: coins is worth 5.
- Add 3: 5 and 3 make 8.
- 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.
Example 3
Gus writes: set a to 5, set b to a, add 2 to a. What are a and b worth?
| Step | Block | a | b |
|---|---|---|---|
| 1 | set a to 5 | 5 | |
| 2 | set b to a | 5 | 5 |
| 3 | add 2 to a | 7 | 5 |
- a holds 5.
- b holds a copy of the value of a: 5.
- 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.
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 →