Combining sequence, selection and iteration in one algorithm

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Combining sequence, selection and iteration in one algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should Count <- 0 be written in an algorithm that counts passes in a FOR loop?
A Before the loop starts
B Inside the loop, on the first line
2. An algorithm must count how many of 30 marks are 50 or more. Which structure goes inside the loop?
A IF Mark >= 50 THEN Count <- Count + 1 ENDIF
B OUTPUT Count <- Count + 1 for every mark
3. Which statement is true about the three constructs in a complete algorithm?
A Selection repeats the input, iteration sets the counters, sequence decides which values to total
B Sequence sets up and outputs, iteration repeats, selection decides inside the loop
4. Where should OUTPUT "Passes: ", Count go if you want the final total of passes?
A After ENDFOR, once the loop has finished
B Inside the loop, before NEXT
5. Which is the correct order of the parts of a totalling algorithm?
A Loop with INPUT and IF, Total <- 0, then OUTPUT Total
B Total <- 0, loop with INPUT and IF, then OUTPUT Total

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Count <- 0 Total <- 0 AFOR Student <- 1 TO 30 INPUT Mark NEXT Student
2IF Mark >= 50 THEN Passes <- Passes + 1 ENDIF BSelection inside the loop: only count the values that meet the condition
3Iteration repeats the input and processing steps a set number of times COUTPUT is inside the loop, so it prints ten times. The result should be output once after NEXT.
4Results are output after the loop has finished, as the last part of the sequence DSequence: set the counter and total to 0 before the loop starts
5FOR i <- 1 TO 10 Count <- 0 INPUT Num IF Num > 5 THEN Count <- Count + 1 ENDIF NEXT i ECount is set to 0 inside the loop, so it is reset on every repeat and the final count is wrong. It must be set to 0 before the loop.
6FOR i <- 1 TO 10 INPUT Num Total <- Total + Num OUTPUT Total NEXT i FNEXT Student OUTPUT "Passes: ", Passes

Definitions

sequence, selection, iteration

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
Total ← _ FOR Index ← 1 TO 5 INPUT Mark
2.
FOR Student ← 1 TO 30 INPUT Mark __ Mark >= 50 THEN
3.
IF Mark >= 50 THEN Passes ← Passes _ 1 ENDIF
4.
NEXT Index OUTPUT "Total: ", _____
5.
IF Mark > 50 THEN Total ← Total + ____ ENDIF

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Total <- 0
B FOR i <- 1 TO 5
C READ Price
D IF Price < 10 THEN
E Total <- Total + Price
F ENDIF
Question 2
A Total <- 0
B FOR i <- 1 TO 8
C INPUT Temp
D IF Temp > 30
E Total <- Total + Temp
F ENDIF
Question 3
A Count = 0
B FOR i <- 1 TO 20
C INPUT Age
D IF Age >= 18 THEN
E Count <- Count + 1
F ENDIF
Question 4
A IF Mark => 50 THEN
B Pass <- Pass + 1
C ENDIF
D NEXT i
E OUTPUT Pass
Question 5
A FOR i <- 1 TO 10
B INPUT Mark
C IF Mark >= 50 THEN
D Count <- Count + Mark
E ENDIF
F NEXT i

Answer Key

Combining sequence, selection and iteration in one algorithm
Section A: Quick Questions
Q1
A — Before the loop starts
Q2
A — IF Mark >= 50 THEN Count <- Count + 1 ENDIF
Q3
B — Sequence sets up and outputs, iteration repeats, selection decides inside the loop
Q4
A — After ENDFOR, once the loop has finished
Q5
B — Total <- 0, loop with INPUT and IF, then OUTPUT Total
Section B: Match the Pairs
Q1
Count <- 0 Total <- 0 → Sequence: set the counter and total to 0 before the loop starts
Q2
IF Mark >= 50 THEN Passes <- Passes + 1 ENDIF → Selection inside the loop: only count the values that meet the condition
Q3
Iteration repeats the input and processing steps a set number of times → FOR Student <- 1 TO 30 INPUT Mark NEXT Student
Q4
Results are output after the loop has finished, as the last part of the sequence → NEXT Student OUTPUT "Passes: ", Passes
Q5
FOR i <- 1 TO 10 Count <- 0 INPUT Num IF Num > 5 THEN Count <- Count + 1 ENDIF NEXT i → Count is set to 0 inside the loop, so it is reset on every repeat and the final count is wrong. It must be set to 0 before the loop.
Q6
FOR i <- 1 TO 10 INPUT Num Total <- Total + Num OUTPUT Total NEXT i → OUTPUT is inside the loop, so it prints ten times. The result should be output once after NEXT.
Section C: Definitions
Q1
sequence, selection, iteration — Set counters to 0 first (sequence), loop to read input (iteration), use IF inside the loop to pick values to count or total (selection), then OUTPUT the result after the loop.
Section D: Fill in the Blanks
Q1
Total ← 0 FOR Index ← 1 TO 5 INPUT Mark
Q2
FOR Student ← 1 TO 30 INPUT Mark IF Mark >= 50 THEN
Q3
IF Mark >= 50 THEN Passes ← Passes + 1 ENDIF
Q4
NEXT Index OUTPUT "Total: ", Total
Q5
IF Mark > 50 THEN Total ← Total + Mark ENDIF
Section E: Spot the Error
Q1
C — Cambridge pseudocode reads a value with INPUT, not READ. The line should be INPUT Price, so each pass of the loop reads one price.
Q2
D — An IF statement needs THEN after its condition. The line should be IF Temp > 30 THEN, so the selection decides which temperatures are added to Total.
Q3
A — Assignment uses <-, not =. In Cambridge pseudocode = is a comparison. The counter must be set with Count <- 0 before the loop starts.
Q4
A — There is no => operator. 'Greater than or equal to' is written >=, so the line should be IF Mark >= 50 THEN.
Q5
D — To count the passes, Count must go up by 1 each time, not by the mark. The line should be Count <- Count + 1. Adding Mark would be totalling.