AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Build it yourself

Week 4 · 1.3 Creating algorithms · 3 lessons
1

Plan a decision

Computing 9: Topic 1.3, pp. 46–79
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.

StepQuestion to askOur school bag example
1What is the condition?kilos > 5
2What runs if it is true?OUTPUT "Too heavy for a school bag"
3What runs if it is false?OUTPUT "Bag is fine"
Worked examples
StartINPUT kiloskilos > 5?yesnoOUTPUT "Too heavy for a school bag"OUTPUT "Bag is fine"End
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.
ProblemCondition
Tickets are for children aged 6 or overage >= 6
The pool closes if more than 40 swimmers are in itswimmers > 40
The lunch money is exactly 3money = 3
The battery is nearly empty: 15 or lessbattery <= 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.
StepYour answer
1 The condition?
2 Runs if true?
3 Runs if false?
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.
StartINPUT guestsguests > 6?yesnoOUTPUT "Use the big table"?End
3
Write pseudocode for this flowchart.
StartINPUT rainrain >= 10?yesnoOUTPUT "Take an umbrella"OUTPUT "Leave it at home"End
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.
Pseudocode
1INPUT battery
2IF battery <= 15 THEN
3    OUTPUT "Battery fine"
4ELSE
5    OUTPUT "Charge now"
6ENDIF