GoalExplain selection, use a decision diamond with yes and no branches, and trace a flowchart that makes a choice.
New words
selection · a point in a program where a choice is made between two or more pathscondition · a question that is either true or false, such as age < 8sequence · steps that are carried out one after the other, in orderdecision symbol · a diamond in a flowchart that holds a condition and has two ways out, yes and no
Explanation
A program with only a sequence does the same steps every time. Many programs have to make a choice, and that is called selection. A selection starts with a condition, which is a question that can only be answered true or false. In a flowchart the condition goes in a diamond. The diamond has two arrows out of it: one for yes (true) and one for no (false). The program follows only one of them. Afterwards, the paths can join again before the stop. To trace a flowchart, choose an input, answer the question and follow the right arrow.
Worked examples
Age typed
age < 8?
Output
6
yes
Shallow pool
8
no
Main pool
30
no
Main pool
This is the pool flowchart traced with three inputs. The condition is age < 8. Each input follows exactly one path.
A branch can hold a process box as well as an output. Here the price is chosen first. Both branches join again, and then the price is shown. For age 9 the output is 5, and for age 12 it is 10.
Construct
What it does
In the ticket flowchart
sequence
steps one after another
INPUT age, then price, then OUTPUT price
selection
chooses one of two paths
the diamond: age < 12?
Every flowchart is built from two constructs. Which one is a step in a row, and which is a choice? Look at the tickets flowchart.
Class activity
Choose a path (no computer): draw a large diamond on a sheet and write the condition ‘Is the number less than 10?’ in it. Draw a yes arrow to the left and a no arrow to the right. On the left write ‘hop on one foot’ and on the right ‘clap twice’. A partner thinks of a number and tells you. You put a coin on the diamond and move it along the correct arrow, and do the action. Try the numbers 4, 10 and 25. Then make your own diamond with a different question. If you have a computer or tablet, draw your flowchart in a drawing program.
Practice
1
Trace the flowchart with 14 items and then with 10 items. What is output each time?
With 14 items the condition items > 10 is yes, so it outputs ‘Use a trolley’. With 10 items the condition is no, because 10 is not more than 10, so it outputs ‘Use a basket’.
2
Mia’s flowchart should say ‘Too hot’ for temperatures over 35. Find the mistake and say how to fix it.
The branches are swapped: a hot day follows the yes arrow, and the yes arrow leads to ‘Comfortable’. Fix: swap the two output boxes.
3
Draw a flowchart for this: the user enters the number of players. If there are at least 11 it outputs ‘Match can start’; otherwise it outputs ‘Need more players’.
Start, INPUT players, a diamond players >= 11?, the yes arrow to OUTPUT ‘Match can start’, the no arrow to OUTPUT ‘Need more players’, then Stop.
4
In the ticket flowchart in example 2, name the sequence part and the selection part. Why can the two branches join again before OUTPUT price?
Sequence: INPUT age at the start and OUTPUT price at the end. Selection: the diamond age < 12?. The branches can join because after a price is chosen, both paths need the same last step, which is to show the price.