Could you print a label for every passenger on a Galway train with one print line?
A school group is taking the train from Galway. Each passenger needs a numbered label. There are three passengers on your list now, but what if there were thirty? Could you print every label without copying the print line?
Start with one passenger. Read the pseudocode from the top: if their ticket is valid, you print their passenger number. If the ticket is invalid, you skip the print instruction. That's selection: a condition decides whether an action runs.
The tickets have been checked. Now watch the passenger number take the values one, two and three. For each value, the indented line prints one label. This is a for loop. Four is the stop value of the range, so it never gets printed.
The same print line now sits inside a for loop that uses range from one to three. How many times will the print line run? Pick an answer before you move on.
Trace the loop with a range from two to five. The range gives you two, three and four. Follow the print instruction for two, then for three, then for four. Write each value on its own line to show the output exactly.
Keep the print line the same, but change the range to three to seven. Which values appear in the output, in order? Pick the list that matches what the loop prints.
Aoife and Niamh are in the queue. Check whether any names remain. Take out the first name and print it, then go back and check again. When the queue is empty, the test is false and you stop. This is a while loop.
Put the while loop inside a function called print labels. The call at the bottom gives it the queue. The loop checks the length, then pop zero removes the first name and gives it back to print. You get Aoife, then Niamh. The next check is false.
Both loops start with names in the queue and check whether any remain. On the left, printing leaves the queue unchanged, so the test stays true and the same name prints again. On the right, removing each name before printing it eventually makes the test false.