AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Two tests, one decision

Week 4 · 1.2 Selection in pseudocode · 3 lessons
3

Ranges and many inputs

Computing 8: Topic 1.2, pp. 27–58
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 typedseat < 1seat > 40Output
0TrueFalseInvalid seat
1FalseFalseFront
20FalseFalseFront
21FalseFalseBack
40FalseFalseBack
41FalseTrueInvalid 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.
markmark > 0mark < 100with ORwith AND
−5FalseTrueTrueFalse
50TrueTrueTrueTrue
150TrueFalseTrueFalse
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.
seatseat < 1 OR seat > 40NOT (seat >= 1 AND seat <= 40)
0TrueTrue
12FalseFalse
41TrueTrue
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
2
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
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.
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.