GoalWrite range conditions with AND and OR and trace an algorithm with several different inputs.
New words
range check · a test that a value is inside, or outside, a set of limitsinclusive · including the limit itself, so 1 to 40 includes both 1 and 40test data · values chosen to try out an algorithm
Explanation
A range has a lowest and a highest value. To test that a value is inside a range, use AND: the value must be at least the lowest AND at most the highest. To test that it is outside, use OR: the value is below the lowest OR above the highest. A very common mistake is to use OR for inside, such as mark > 0 OR mark < 100. Every number passes this test, because every number is either above 0 or below 100. To trace an algorithm with several inputs, make a table with one row for each input, and write the result of each small condition and the output. Always include the values on and just beside the limits.
Pseudocode
1INPUT seat
2IF seat < 1 OR seat > 40 THEN
3 OUTPUT "Invalid seat"4ELSEIF seat <= 20 THEN
5 OUTPUT "Front"6ELSE
7 OUTPUT "Back"8ENDIF
Worked examples
Seat typed
seat < 1
seat > 40
Output
0
True
False
Invalid seat
1
False
False
Front
20
False
False
Front
21
False
False
Back
40
False
False
Back
41
False
True
Invalid seat
A coach has 40 seats and the front half is seats 1 to 20. OR catches the numbers outside the range. These are the traced results for six inputs.
mark
mark > 0
mark < 100
with OR
with AND
−5
False
True
True
False
50
True
True
True
True
150
True
False
True
False
Why OR is wrong for ‘inside the range’. For mark > 0 OR mark < 100, every input gives True. With AND, only the numbers from 1 to 99 give True.
seat
seat < 1 OR seat > 40
NOT (seat >= 1 AND seat <= 40)
0
True
True
12
False
False
41
True
True
NOT can turn a range test around. NOT (seat >= 1 AND seat <= 40) gives exactly the same answers as seat < 1 OR seat > 40. Both are shown here for the same inputs.
Class activity
Test the limits (no computer). Pick a range such as ‘ages 9 to 13 inclusive’. Write the range test twice: once for inside using AND, and once for outside using OR. Make a test table on paper with ten ages that include 8, 9, 13 and 14. Ask a partner to call the ages while you fill in the table with True or False for each small condition. Swap roles with a new range, for example ‘a score of 1 to 5’. If you have a computer or tablet, ask an adult to run both tests to see if the answers match your table.
Practice
1
Follow the algorithm for the temperatures 0, 17, 18, 26 and 27. What is shown each time?
Pseudocode
1INPUT temp
2IF temp >= 18 AND temp <= 26 THEN
3 OUTPUT "Comfortable"4ELSEIF temp < 18 THEN
5 OUTPUT "Cool"6ELSE
7 OUTPUT "Hot"8ENDIF
This validation always says Valid, even for 150. Explain why and write the corrected line.
Pseudocode
1INPUT mark
2IF mark > 0 OR mark < 100 THEN
3 OUTPUT "Valid"4ELSE
5 OUTPUT "Invalid"6ENDIF
Every number is above 0 or below 100, so the OR condition is always true. A mark that is inside the range must be above 0 AND below 100 (or mark >= 0 AND mark <= 100 if 0 is allowed).
3
Write pseudocode: the user types a score. If it is from 0 to 10 inclusive show Valid, otherwise show Invalid. Then write the same condition again using OR and NOT.
IF score >= 0 AND score <= 10 THEN OUTPUT "Valid" ELSE OUTPUT "Invalid" ENDIF. With OR and NOT: IF NOT (score < 0 OR score > 10) THEN, with the same two outputs.
4
A pupil writes: IF day = "Mon" AND day = "Tue" THEN. Explain why this condition is never true and fix it so that it is true for Monday or Tuesday.
A variable holds only one value at a time, so day cannot equal both Mon and Tue. Use OR: day = "Mon" OR day = "Tue".