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

Term 1 in review

Week 11 · Review: Term 1 · 3 lessons
1

Read, repair, extend

Computing 9: Topics 1.1–1.7, pp. 11–134
GoalReview pseudocode, loops and flowcharts: follow an algorithm, find and repair a bug, and write a new algorithm of your own.
New words
pseudocode · a plan for a program written in clear, structured words; a computer does not run itcount-controlled loop · a loop that repeats a set number of times, using a counterlogic error · a mistake that lets an algorithm run but gives the wrong resulttrace table · a table in which you record the value of each variable as you follow an algorithm
Explanation

Every algorithm is built from three constructs: sequence, selection and iteration. A sequence runs lines in order. A selection (IF … ENDIF) chooses between paths, and an iteration (a loop) repeats lines. A FOR loop has a counter, a start value and an end value, and its body runs once for each value from the start to the end. To find out what an algorithm does, follow it line by line and write the variables in a trace table. To change an algorithm, edit the smallest part that must change and then test it again. A bug that does not stop the algorithm is a logic error, and the usual places to look are the start and end values of a loop, the order of lines and the comparison in an IF.

ConstructPseudocode shapeWhat it does
Sequenceline 1, line 2, line 3runs the lines in order, once
SelectionIF … THEN … ELSE … ENDIFchooses one path
IterationFOR counter ← 1 TO 5 … NEXT counterrepeats the body a set number of times
Worked examples
Pseudocode
1INPUT n
2total ← 0
3FOR k ← 1 TO n
4    INPUT price
5    total ← total + price
6NEXT k
7IF total > 50 THEN
8    total ← total - 5
9ENDIF
10OUTPUT "Pay ", total
Output
Pay 58
Following an algorithm with a loop. A kite seller adds up the prices and takes 5 off a total over 50. The user types 3, then the prices 20, 18 and 25. The running total is 20, 38 and then 63. The IF is true because 63 > 50, so the total becomes 58.
Pseudocode
1FOR day ← 0 TO 5
2    OUTPUT day * 3
3NEXT day
Repairing a loop. A sunflower grows 3 cm a day, and the algorithm should show its height on days 1 to 5. The loop below starts at 0, so it shows 0, 3, 6, 9, 12 and 15. That is six lines, and the first is not wanted, so the output is wrong. Starting the counter at 1 outputs 3, 6, 9, 12 and 15.
Python
1for day in range(1, 6):
2    print(day * 3)
Output
3
6
9
12
15
The same loop in Python. The counter starts at 1 and the loop must stop after 5, so the stop value of range is 6. Note that range stops before its stop value. The program prints 3, 6, 9, 12 and 15, one per line.
Class activity

Fault finders (no computer). Write a pseudocode algorithm of 5 or 6 lines that uses a FOR loop, for an invented job such as listing the days of a camp. Show it to your partner with the input you used. Your partner traces it in a trace table and checks the output. Now each of you adds one logic error to your own algorithm without saying where, for example changing the start value of the loop. Swap again, and find the bug with a new trace.

Practice
1
The user types 4. What does this algorithm output?
Pseudocode
1INPUT lanes
2FOR lane ← 1 TO lanes
3    IF lane = 3 THEN
4        OUTPUT "Lane ", lane, " closed"
5    ELSE
6        OUTPUT "Lane ", lane, " open"
7    ENDIF
8NEXT lane
2
Follow the flowchart. What is output? Then change one box so that it outputs 20, 40 and 60.
Startcounter ← 1counter <= 3?yesOUTPUT counter * 10counter ← counter + 1noEnd
3
Write pseudocode. The user types four temperature readings, one at a time. Output the lowest reading. What does your algorithm output for 9, 4, 7 and 5?
4
Write this Python program in pseudocode, then say what it prints.
Python
1total = 0
2for n in range(1, 5):
3    total = total + n * 2
4print(total)