Programming Concepts

Check your understanding - Repetition and Selection

In the pseudocode below, how would you get the behaviour we want and avoid a spillage for a container that has a capacity of 1000?

Set contents to 100
Set capacity to 1000
Set pigmentamount to 100
Set full to False
Repeat until full is True
    Add pigmentamount to contents
    Stir
    If contents > capacity then set full to True
End repeat

No. This is redefining the problem, not fixing the code!
No. This has no effect on the logic of the loop.
No. Although this gives you the correct behaviour here, it could lead to problems if we decided to change the amount we add to contents each time through the loop.
No. This is closer to what we want, but could still lead to a spillage.
Yes. By moving the test to the end of the loop, and checking that we have sufficient capacity left before adding pigmentamount we avoid the risk of a spillage. The logic will also work if we change the initial values of contents, capacity and pigmentamount. There are many ways of setting up loops and you should choose one that satisfies the logic of the problem.
← Previous 1 2 3 4 5 6 7 8 9 10 11 12 Next →