Control Flow Lab
Sequence, selection and iteration, and the operators that drive them. Built around the fact that this specification does not give you the reference language in the exam, and around the place the marks actually go: evaluating a compound condition exactly.
Get the method right under pressure
Free interactive practice on the steps that lose marks under exam pressure.
Start revising freeWhat you'll cover
Written from memory
One thing about this specification changes how you should revise this topic. The Exam Reference Language is not given to you in the exam. You are not reading the syntax off a sheet and picking the right line: you are producing it from memory, under time pressure. That has a second consequence people miss. Almost nobody loses marks on the three constructs themselves, because sequence, selection and iteration are easy to describe. The marks go on the conditions inside them, and on evaluating a compound condition exactly rather than roughly. This module covers the constructs briskly and then spends its time where the marks are.
The three constructs and their loops
Six terms. The last three are about the conditions, which is where this topic gets difficult.
Name the construct
A program keeps asking for a password until the one entered is not wrong. Which construct is this?
- Condition-controlled iteration, because it repeats until a condition changes
- Count-controlled iteration, because it repeats a set number of times
- Selection, because a condition is being tested to make a choice
- Sequence, because the instructions are carried out one after another
Construct to what it does
- sequence
- selection
- count-controlled iteration
- condition-controlled iteration
- carries out each instruction once, in the order written
- tests a condition and takes one path or another
- repeats a known number of times, decided before the loop starts
- repeats an unknown number of times, until a condition changes
Counting, or waiting for a condition
Choosing the wrong loop is a common error and an easy one to avoid. Ask one question: before the loop starts, do you already know how many times it will run?
Constructs in a paragraph
Instructions carried out one after another form a _____. When a program tests a condition and takes one path or another, that is _____. Repeating a block is _____. If the number of repeats is known before the loop starts, a _____-controlled loop is the right choice; if it is not known, the loop must keep going while a _____ stays true.
Where the marks actually go
The relational operators for this specification are equals, not equal to written as an exclamation mark and an equals sign, less than, greater than, less than or equal to, and greater than or equal to. The Boolean operators are AND, OR and NOT. Learn them as a closed list, because you will be writing them from memory. Then learn the rule that catches most people. AND needs BOTH sides true. OR needs at least ONE side true. NOT flips whatever follows it. Reading a compound condition roughly, in the way you would read English, is how correct-looking answers turn out wrong.
Evaluate the condition
A variable named score holds 7 and a variable named lives holds 0. Select the TWO conditions below that are true.
- score > 5 OR lives > 0
- NOT lives > 0
- score > 5 AND lives > 0
- score < 5 OR lives != 0
Complete the condition
A game ends when the player runs out of lives, or when the score reaches the target. So the loop must keep going while BOTH of those are still false. Complete the pseudocode. WHILE lives _____ 0 _____ score _____ target ... the loop body runs ... ENDWHILE. Afterwards, a message for anyone who either reached the target or still had lives left reads IF score _____ target _____ lives > 0 THEN OUTPUT well played ENDIF.
What NOT does
A variable named fuel holds 3. What is the value of NOT fuel > 5?
- True, because fuel is not greater than 5, and NOT flips that false to true
- False, because fuel is less than 5 and NOT makes a smaller value false
- True, because NOT changes the greater than into a less than sign
- Neither, because NOT can only be applied to a whole loop condition
Assemble the loop
These five lines make one working count-controlled loop that adds the numbers 1 to 5. Put them in order.
- total <- 0
- FOR i <- 1 TO 5
- total <- total + i
- NEXT i
- OUTPUT total
A compound condition, worked
Question: a variable named speed holds 40 and a variable named gear holds 3. State whether the condition speed > 30 AND NOT gear < 2 is true, and explain your working. Model answer: take the condition in parts. First, speed > 30. Speed is 40, so this part is true. Second, gear < 2. Gear is 3, so this part is false. NOT is applied to that, so NOT gear < 2 gives true. The whole condition is now true AND true, and AND requires both sides, so the condition is true. Notice the method. Each comparison is evaluated on its own first, NOT is applied to whatever immediately follows it, and only then is AND applied to the two results. Working left to right in one pass, as you would read a sentence, is what produces wrong answers here.
Trace the loop
Dry-run this algorithm and fill in the blank cells. Watch the first pass carefully: the condition is not met every time.
Find the faulty line
This code should end the game when the player has no lives left OR when the score reaches the target. Select the ONE line that does not do what was asked.
- lives <- 3
- WHILE lives > 0 AND score < target
- score <- score + 1
- OUTPUT game over
Explain the constructs
A program should keep asking a player for a guess until they either guess correctly or run out of attempts. Explain how you would write it, and how you would check your condition is right.
- Name the construct you would use to repeat the guessing, and say why that one rather than the other loop
- State the condition that would keep the loop running, using operators from this specification
- Explain which Boolean operator joins the two parts of that condition, and why it is that one and not the other
- Explain how you would evaluate the condition exactly, taking it in parts rather than reading it straight through
- Say why being able to write the reference language from memory matters in this exam