How does a school race app know when to say ‘Well done’ instead of ‘Keep going’?
You're doing a school fun run in Leeds. While you're still running, the app says ‘Keep going’. Once you've crossed the finish line, it says ‘Well done’. How can one piece of code choose which message you see?
You already know that a variable stores a value. Here, the name ‘finished’ tells you what the value means. True means the runner has finished. False means they haven't. A variable used like this is called a Boolean flag.
The first line sets finished to true. When the code checks finished, it follows the first branch and displays ‘Well done’. It skips the else branch. ‘End if’ marks where the branches end. Choosing a branch based on a condition is called selection.
Now the runner is still on the course, so finished is false. The two messages are in the same branches as before. Which message will the app display this time? Pick one answer.
Set finished to false. When the code checks finished, it takes the else branch and displays ‘Keep going’. ‘End if’ marks the end of the selection. The next instruction displays ‘View timer’. It's outside both branches, so either route reaches it.
Start with finished set to true. The condition is true, so the app displays ‘Well done’, then continues past ‘end if’ to display ‘View timer’. Start again with false. This time else gives you ‘Keep going’, then the same shared instruction displays ‘View timer’.
A cycling app displays ‘Ride complete’ if finished is true, or ‘Still riding’ in the else branch. After ‘end if’, it displays ‘View map’. If finished is false, which messages appear, and in what order? Pick one answer.
If you put ‘View timer’ inside the else branch, it only appears when finished is false. Move that output below ‘end if’ and line it up with the opening if. Now it appears after either branch.
If you're asked to write pseudocode for a quiz app, test finished. Indent ‘Quiz complete’ in the if branch and ‘Keep answering’ in else. Close the selection with ‘end if’. Put ‘View score’ below it, aligned with if, so it appears in both cases.