Week 6 · Unit 7.2: Decomposing problems · 3 lessons
3
Questions with two answers
Computing 7: Unit 7.2, pp. 44–81
GoalRead and draw a flowchart that uses selection to decide what to do.
New words
flowchart · a diagram of an algorithm made from shapes joined by arrowsselection · a choice in a program: the path depends on whether a question is true or falsedecision · the diamond in a flowchart that asks a yes or no questioncondition · the question or test inside a decision, such as “Is A pressed?”
Explanation
An algorithm is a list of precise steps. A flowchart draws an algorithm with shapes: ovals for start and stop, parallelograms for input and output, rectangles for processes and a diamond for a decision. The diamond asks a question that can only be answered yes (true) or no (false). Selection means the program chooses its path using the answer. With no selection, the steps are just a sequence, one after the other. On the micro:bit the question might be “Is button A pressed?” If yes, the outputs happen. If no, nothing happens. Always plan with a flowchart first. On paper it is quick to see whether the steps and the question are in a sensible order.
Worked examples
This flowchart asks one question. If the touch logo is touched, the board shows a star and then scrolls the word Hi. If it is not touched, nothing happens.
Situation
Path through the flowchart
Result
logo is touched
start, yes, show star, scroll Hi, end
a star, then Hi
logo is not touched
start, no, end
nothing happens
Following a flowchart with a situation is called tracing it. For the flowchart above we can trace two situations and write down the path and the result.
Sequence
Selection
shape that is new
none
diamond
steps that happen
all of them
depends on yes or no
example
scroll a word, show a picture
if a button is pressed, show a picture
A sequence has no choice: every step happens, in order. A selection has a diamond, and only one path is followed each time. The words in the flowchart say what the program does, not what the program looks like when typed.
Class activity
Human flowchart (no computer). Put three sheets of paper on the floor: the diamond “Is the card red?”, a “yes” sheet that says “clap twice” and a “no” sheet that says “stamp once”. An adult turns over ten playing cards one at a time and you follow the flowchart. Count the claps and the stamps. Then change the question to “Is the card a picture card?” and play again. What stayed the same, and what changed?
Practice
1
In this flowchart, what happens when button B is not pressed? What happens when it is pressed?
Not pressed: nothing is shown and the program ends or waits. Pressed: the board scrolls the word Stop and then shows a cross.
2
Draw a flowchart for this plan: if button A is pressed, show an arrow and then scroll the word Bus.
Start (oval), then a diamond “Is A pressed?”. From yes: a parallelogram “show an arrow”, then a parallelogram “scroll the word Bus”, then end. From no: straight to end.
3
These steps are jumbled. The plan scrolls Done first and then shows a tick. Put the steps in the right order for the flowchart: (a) show a tick, (b) Start, (c) scroll Done, (d) Is A pressed?, (e) End.
Letter
Step
a
show a tick
b
Start
c
scroll Done
d
Is A pressed?
e
End
b, d, c, a, e. The question comes before the outputs, and the outputs only happen on the yes path.
4
Why is it a good idea to draw the flowchart before you write the program?
With a flowchart you can look at the whole plan on one page. A wrong question or a step in the wrong place is easy to spot and rub out. Typing code only starts once the plan looks right.