Programs that Remember and Choose
Combining conditions with and, or and not
Work out whether a condition joined by and, or or not comes out true, and which branch a program takes because of it.
One test is often not enough. A programmer wants to know whether a number is inside a range, or whether either of two things has gone wrong, and neither of those is a single comparison. Three little words do the joining: and, or and not. Each of them takes conditions that are already true or false and produces one answer out of them.
How demanding each word is
And is the strict one: it wants every part satisfied, so a single failing part is enough to sink the whole condition. Or is the generous one: one satisfied part carries the whole thing, and it stays true even when both are satisfied. Not takes a single condition and hands back the opposite. The way to use any of them is always the same — work each part out on its own first, write down true or false for it, and only then let the joining word decide.
Worked example
A ride is open to anyone who is at least 8 years old and is holding a ticket. A rider is 10 years old and has lost their ticket. Can they ride?
Split the condition at the joining word. The two parts are: at least 8 years old, and holding a ticket.
Splitting first is what stops two separate tests from being muddled into one impression of the rider.
Try it together
Now work this one out together: a book is overdue if it was borrowed more than 14 days ago or it has been reserved by somebody else. A book was borrowed 3 days ago, and somebody has reserved it.
Take each part on its own, then let the joining word decide.
1.Is the first part true? Was it borrowed more than 14 days ago — answer yes or no.
Have a go
Have a go on your own. A program says: if the number is more than 0 and less than 100, print "Inside"; otherwise print "Outside". The number is 100. What does it print?
Ready to practise?
Eight questions on what you have just read. Nothing is timed, and you can play as many times as you like.