CS10-06 Computer Science Watch
Sequence, selection and iteration in an algorithm
In this lesson
Sequence: how to name the three constructs that control the flow of an algorithm, with worked examples and the mistakes examiners report. Edexcel 1CP2 and OCR J277 GCSE. Algorithms 6 of 9. More at https://scholafly.com
By the end: Name the three constructs that control the flow of an algorithm - sequence, selection and iteration - point to each one in a short algorithm by its full range of lines, and, for Edexcel, tell repetition (count-controlled and condition-controlled loops) apart from iteration over every item in a data structure.
What it covers
- 0:00 Intro: sequence, selection, iteration
- 0:32 Sequence, selection, iteration: three constructs
- 2:55 A construct is a whole block
- 4:35 Two kinds of loop
- 7:11 Everyday examples
Key words
About this video
Sequence, selection and iteration in an algorithm | GCSE Computer Science | CS10-06
Sequence: how to name the three constructs that control the flow of an algorithm, with worked examples and the mistakes examiners report.
Edexcel 1CP2 and OCR J277 GCSE. Algorithms 6 of 9.
More at https://scholafly.com
Watch first: CS10-05 - https://youtu.be/Ioa4-no7NkE
Video code: CS10-06 (search "ScholaFly CS10-06")
#GCSEComputerScience #Algorithms
For teachers
This GCSE Computer Science lesson teaches sequence, selection and iteration in an algorithm. By the end, students should be able to name the three constructs that control the flow of an algorithm - sequence, selection and iteration - point to each one in a short algorithm by its full range of lines, and, for Edexcel, tell repetition (count-controlled and condition-controlled loops) apart from iteration over every item in a data structure. It works through four worked examples and the mistakes examiners report.
Exam board specification references:
Edexcel 1CP2
- 1.2.1
- 6.2.2
OCR J277
- 2.2.1
Read the transcript
From a calculator to a video game, how many different kinds of instruction does it take to build a program? It is tempting to guess hundreds, because programs are huge and do so many different things. The answer is just three, and by the end you can point at any line of a program and say which of the three it is.
Sequence means the steps run one after another, in the order they are written. A construct is one of these basic ways of controlling the order, and sequence is the first. Order is an instruction too. Swap two lines, and the output changes. It is the same sequence as in the definition of an algorithm from the video What an algorithm is. Selection means a condition decides which steps run. IF, ELSE and ENDIF build it, and the selection is the whole block, from the IF line down to its ENDIF. Iteration means steps repeat in a loop. On most courses, iteration and repetition are two words for the same thing. One course splits them, and that comes later in this video. Each construct has its own look on a flowchart. Take the password flowchart from the video Flowcharts: input password, a diamond asking if it is letmein, and output Welcome. In that password flowchart, which part is the iteration? The arrow back up from the no exit. It sends the algorithm round again, so the input repeats until the password is right. The diamond and its two exits are the selection. The run from start to input is sequence, a straight line of boxes. One examiner's report notes: "This question was answered very well by the majority of candidates, showing a good understanding of the difference between the key programming constructs of selection and iteration." The habit that keeps that understanding scoring is precision: the whole block, and the right word for each loop.
Questions can ask for a construct by its line numbers, and a construct is a whole block, from its first line to its end marker. This twelve-line algorithm adds up scores until a nought is typed, outputs the total, then says whether a target of a hundred was met. Which lines are the loop, and which are the selection, first line to end marker? The loop is lines three to six, WHILE to ENDWHILE. The selection is lines eight to twelve, IF to ENDIF. Lines one, two and seven are plain sequence. One examiner's report, on a longer algorithm, records this slip: "The most common mistake was not identifying the entire selection process and instead only listing the IF command on line 16." The fix is to give the full range. Selection runs from the IF line to its ENDIF, and every line in between belongs to it. One course also asks that each block has a single way in and a single way out, at its top and its bottom. That is good practice on any course, because it keeps each block clear to follow.
Loops come in two kinds. A count-controlled loop repeats a known number of times, and a condition-controlled loop repeats until something changes. Four loops to sort. A, repeat ten times. B, keep asking until the answer is yes. C, for each name in the list. D, while the tank is not full. Which of the four are count-controlled, and which are condition-controlled? A and C are count-controlled. C runs once for each name, a number known before it starts. B and D are condition-controlled. One course, though, gives a loop over every item in a collection its own name. On that course, repetition means count-controlled and condition-controlled loops. Iteration means going over every item in a data structure, a collection of data such as a list, one item at a time. Other courses use the two words as one. One examiner's report says so plainly: "Iteration is commonly referred to as loops, or repetition." On the course that splits the words, a report records: "Others confused repetition and iteration." So here is the rule. On the course that splits the words, a for-each over a list is iteration, and every other loop is repetition. Everywhere else, iteration means any loop. A program goes through five names stored in a list and prints every one of them in capitals. On the course that splits the words, is that repetition or iteration? And elsewhere? Iteration, because it goes over every item in a data structure. Any other course would also call it iteration, or a loop, or repetition, and mean the same thing.
Naming the three constructs is the recall question, and an everyday example for each shows you have the idea, not only the words. What are the three constructs, with an everyday example of each from outside computing? Sequence: a recipe's steps, in order. Selection: wear a coat if it is raining. Iteration: stir until the sauce is smooth.
Three constructs, whole blocks and the split words: the questions run in that order. A FOR loop starts on line four and its NEXT is on line nine. What range do you give? Four to nine: the FOR line, its NEXT, and every line between. Now a loop that stops once a password is right: count-controlled or condition-controlled? Condition-controlled. Nobody knows in advance how many tries it will take. Here's one on the split words: is repeat five times repetition or iteration? Repetition. It is count-controlled, and it goes over no data structure. One more: a calculator adds two numbers, then shows the total only if it is positive. Which constructs? Sequence for adding, then showing in order, and selection for the check on whether the total is positive. There is no iteration, because nothing repeats, so this small calculator program needs just two of the three.
This video now has its place in the list. When this one's yours, hit thumbs up to tick it off for good; a blank one is a video to come back to. If you are still unsure, save it for now. The next video, Trace tables, runs loops and selections line by line, and that makes these three names concrete.
Next in the chapter: Trace tables, following an algorithm one line at a time. Hopefully that's next in your feed, but if not, search for ScholaFly C S ten, oh seven.
Every other video is on scholafly.com too, easy to search by video code or by keyword. Whatever you're revising, go there and have a look, or, watch the next video.
Related terms
For: Edexcel GCSE 1CP2, OCR GCSE J277
On the specification
For teachers
This GCSE Computer Science lesson teaches sequence, selection and iteration in an algorithm. By the end, students should be able to name the three constructs that control the flow of an algorithm - sequence, selection and iteration - point to each one in a short algorithm by its full range of lines, and, for Edexcel, tell repetition (count-controlled and condition-controlled loops) apart from iteration over every item in a data structure. It works through four worked examples and the mistakes examiners report.