DoRevision

Control Flow Lab

Assignment is not an equals sign, and once that is straight everything else follows. What a variable really names, what a constant promises, and how to read nested loops without losing track of which one you are in.

⏱️ 24 min 🎯 15 activities
Best used for
Intervention Mock preparation Cover lesson

Get the method right under pressure

Free interactive practice on the steps that lose marks under exam pressure.

Start revising free

What you'll cover

Not an equals sign

Here is a line that appears in almost every program ever written, and that stops a lot of people in their tracks: count <- count + 1. Read as maths it is nonsense, because nothing equals itself plus one. It is not maths. It is an instruction, and it says take whatever is currently in the box called count, add one to it, and put the result back in the same box. That is assignment, and it is the single idea this whole topic rests on. A variable is a name for a box, not a name for a value, so the contents can change while the name stays put. A constant is the same thing with a promise attached: this one will not change. Once assignment is straight, tracing a program stops being guesswork, because tracing is just watching assignment happen one line at a time. Throughout this module the arrow <- means assignment. Other books and other exam papers write it differently, and part of your job is reading somebody else pseudo-code, so notice the notation and keep going.

Boxes and labels

Five terms. You already know sequence, selection and iteration as the three ways to control what runs; these are about what the code is working on.

What x becomes

A program contains the two lines x <- 5 and then x <- x + 3. What does the second line do?

  • It takes the current contents of x, adds 3, and puts the result back into x, so x becomes 8
  • Nothing, because a value cannot equal itself plus three
  • It creates a second variable also called x, holding 3 more than the first
  • It sets x to 3, because that is the value written on the right

Match the snippet to its result

  • x <- 5 then x <- x + 3
  • x <- 5 then x <- x * 2
  • x <- 5 then y <- x then x <- 2
  • x <- 5 then x <- 5 - x
  • x ends up holding 8
  • x ends up holding 10
  • y ends up holding 5, even though x changed afterwards
  • x ends up holding 0

The trap in the equals sign

Two things follow from assignment being an instruction, and both catch people out. The first is order. In x <- x + 3, the right-hand side is worked out completely, using whatever x holds at that moment, and only then is the answer stored. So the old value is used to make the new one, which is exactly why the line makes sense. The second is that assignment copies a value rather than linking two boxes together. If you write y <- x, then y gets a copy of what x holds right now. Change x on the next line and y does not follow, because nothing connects them any more. Students who expect y to track x get traces wrong in a way that looks like a careless slip and is really a misunderstanding. When you trace, write down what is IN each box after each line, never what it was set from.

Which two are true of constants

Select the TWO statements that are correct about constants.

  • Its value is fixed when it is set and must not change while the program runs
  • Using one makes the intention clear to anyone reading the code later
  • It can hold only whole numbers, unlike a variable
  • It is stored differently so that programs using constants always run faster

Variable or constant

A variable is a name for a _____ whose contents can change while the program runs. A _____ holds a value that must not change once set. The arrow in count <- 5 means _____, not is equal to. When one variable is set from another, the value is _____, so changing the first afterwards leaves the second alone. A trace that shows y changing after x is updated has recorded what y was set from, not what is _____ it.

place constant assignment copied in value variable comparison linked near

Trace the nested loops

Dry-run this algorithm and fill in the blank cells. There is a loop inside a loop, so the inner one finishes completely before the outer one moves on. Tracing is just watching each assignment happen in order.

Outer and inner

A nested structure is nothing new: it is just an instruction that happens to itself be a structure. Reading it confidently comes down to knowing which loop a line belongs to.

Which loop is that line in

Look again at the algorithm you traced. Select the ONE line that runs inside the inner loop, and therefore executes six times in total.

  • count <- 0
  • FOR i <- 1 TO 3
  • count <- count + i
  • OUTPUT count

Build the nested structure

Put these lines in the order that prints every pairing of an outer counter with an inner one.

  • FOR i <- 1 TO 3
  • FOR j <- 1 TO 2
  • OUTPUT i, j
  • NEXT j
  • NEXT i

A nested trace, worked

Here is how to work through the algorithm you just traced, so you can do it with one you have never seen. First, before anything loops, count is set to 0. Now the outer loop starts with i as 1. Inside it the inner loop starts with j as 1, and the line count <- count + i runs: the current count, 0, plus the current i, 1, gives 1, and that goes back into count. The inner loop goes round again with j as 2, and the same line runs again: 1 plus 1 gives 2. The inner loop is finished, so the outer loop moves on and i becomes 2. The inner loop starts over from j as 1, and now the same line adds 2 each time, giving 4 and then 6. With i as 3 it adds 3 each time, giving 9 and then 12. Only when both loops are finished does OUTPUT count run, printing 12. Two habits to take away. Write the value after each line, not the sum you used to get it. And notice that the inner counter resets every time the outer one advances, which is the thing people forget and the reason nested traces go wrong.

Counting or waiting

A program reads numbers typed by a user and stops when the user types zero. Which kind of loop is right, and why?

  • A WHILE loop, because the number of repetitions is not known before it starts
  • A FOR loop, because it will definitely run at least once
  • Either, since any FOR loop can be written as a WHILE loop
  • A nested loop, because two things are being checked

Reading unfamiliar code

You are given an algorithm you have never seen and asked what it outputs. Take the decisions in order.

  • The pseudo-code uses a symbol for assignment you do not recognise. What do you do?
  • You start tracing. What do you write in your table after each line?
  • There is a loop inside a loop. Which counter changes fastest?
  • A line sets one variable from another, and the second changes later. What happened to the first?

Explain assignment and nesting

Explain, as if to somebody who has just started programming, what assignment really does and how to read a loop inside a loop.

  • Explain why count <- count + 1 makes sense, even though it looks impossible as maths
  • Explain the difference between a variable and a constant, and when you would use each
  • Explain what happens when one variable is set from another and the second changes later
  • Explain how many times a line inside an inner loop runs, if the outer loop runs three times and the inner runs twice
  • Explain how you would decide between a FOR loop and a WHILE loop for a task
  • Give one habit that stops a trace table going wrong, and say why it helps