GoalPlan a selection in three steps and then draw it as a flowchart and write it as pseudocode.
New words
selection · a place in an algorithm where a condition decides which steps runcondition · a test that is either true or falsedecision box · the diamond in a flowchart; it holds the condition and has two arrows, Yes and Nodata flow · an arrow in a flowchart that shows which step comes next
Explanation
When you create an algorithm with selection, plan it in three steps. First, find the condition: a test with the answer true or false. Second, identify the steps that run when the condition is true. Third, identify the steps that run when it is false. In a flowchart the test is written as a question inside a diamond. Two arrows leave the diamond, one labelled Yes (true) and one labelled No (false). The steps for each answer go on the matching arrow. Then add the steps that always happen before the decision, such as the start box and INPUT, and the end box, and join the two arrows together again so that every box is joined to another. In pseudocode the same plan becomes IF, ELSE and ENDIF. Plan first, then draw, and then check with a test value on each side of the condition.
Step
Question to ask
Our school bag example
1
What is the condition?
kilos > 5
2
What runs if it is true?
OUTPUT "Too heavy for a school bag"
3
What runs if it is false?
OUTPUT "Bag is fine"
Worked examples
The plan for the school bag turned into a flowchart. INPUT is before the decision because it always runs. With 6 kilos typed the condition is Yes and the output is Too heavy for a school bag. With 5 kilos it is No and the output is Bag is fine, because 5 > 5 is false.
Pseudocode
1INPUT kilos
2IF kilos > 5 THEN
3 OUTPUT "Too heavy for a school bag"4ELSE
5 OUTPUT "Bag is fine"6ENDIF
The same plan as pseudocode, built in stages. First write the skeleton IF … ELSE … ENDIF. Then put in the condition and the two outputs. Last add the INPUT line before it. Indent the branches.
Problem
Condition
Tickets are for children aged 6 or over
age >= 6
The pool closes if more than 40 swimmers are in it
swimmers > 40
The lunch money is exactly 3
money = 3
The battery is nearly empty: 15 or less
battery <= 15
Step 1 is the hardest, so practise it by itself. Words have to be turned into exact conditions. Watch for ‘at most’ and ‘at least’, which include the number.
Class activity
Plan cards (no computer). Write a decision problem on paper, such as ‘If the number of eggs is 12 or more, say Full box, otherwise say Keep packing’. Cut three cards: a condition card, a Yes card and a No card. Shuffle cards from three different problems and race a partner to put each problem together again. Then put a card on the floor for each box of a flowchart and walk it with a number. Finally, write the pseudocode, using IF, ELSE and ENDIF, for one of the problems.
Practice
1
Plan this problem in three steps. A pond sign: the user types the water temperature. If it is below 2 the sign shows Ice risk, otherwise it shows Safe.
Step
Your answer
1 The condition
?
2 Runs if true
?
3 Runs if false
?
Step 1: the condition is temp < 2. Step 2: if true, OUTPUT "Ice risk". Step 3: if false, OUTPUT "Safe".
2
The flowchart should show Use the small table for 6 or fewer guests. What goes in the empty box? Check it with 6 guests and with 7.
OUTPUT "Use the small table". With 6 guests, 6 > 6 is No, so the small table is shown. With 7 guests, 7 > 6 is Yes, so the big table is shown.
3
Write pseudocode for this flowchart.
INPUT rain / IF rain >= 10 THEN / OUTPUT "Take an umbrella" / ELSE / OUTPUT "Leave it at home" / ENDIF, with the OUTPUT lines indented.
4
This algorithm should show Charge now when the battery level is 15 or less. Test it with 10 and find the mistake. Correct it.
With 10, 10 <= 15 is true, so it shows Battery fine, but it should show Charge now. The two outputs are in the wrong branches. Swap them (or change the condition to battery > 15).