Range check

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Range check

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A range check on Age allows values from 0 to 120. Which condition accepts a valid age?
A IF Age >= 0 AND Age <= 120
B IF Age >= 0 OR Age <= 120
2. What does a range check test?
A The value lies between a lowest and a highest allowed value
B The value has been typed in twice with the same result
3. A mark must be between 0 and 100 inclusive. Is a mark of 100 accepted by IF Mark >= 0 AND Mark <= 100?
A No, 100 is rejected because it is on the limit
B Yes, 100 is accepted because <= includes the limit
4. Which condition is TRUE when a mark is outside the range 0 to 100?
A Mark < 0 OR Mark > 100
B Mark < 0 AND Mark > 100
5. A range check allows 1 to 31 for the day of a month. Which condition wrongly rejects the valid value 31?
A IF Day >= 1 AND Day <= 31
B IF Day >= 1 AND Day < 31

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1IF Mark >= 0 AND Mark <= 100 THEN ATests whether Age is outside the allowed range, so the value is rejected
2IF Age < 0 OR Age > 120 THEN BIF Score >= 1 AND Score <= 10 THEN
3A range check on a date: the month number must be between 1 and 12, and 1 and 12 are valid CIF Month >= 1 AND Month <= 12 THEN
4Both limits must be true for a number to pass, so the two tests are joined with AND DThe limits 0 and 100 are wrongly rejected because > and < are used. It should be >= and <= so the limits are accepted.
5IF Age >= 0 OR Age <= 120 THEN EOR is used to join the limits, so every number passes, because any number is either at least 0 or at most 120. It should be AND.
6IF Mark > 0 AND Mark < 100 THEN FRange check: accepts a Mark from 0 to 100, including 0 and 100 themselves

Definitions

IF Age >= 0 AND Age <= 120

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
IF Age >= 0 ___ Age <= 120 THEN OUTPUT "Age accepted" ENDIF
2.
IF Age >= 0 AND Age __ 120 THEN OUTPUT "Age accepted" ENDIF
3.
IF Age < 0 __ Age > 120 THEN OUTPUT "Invalid age" ENDIF
4.
IF Score __ 0 AND Score <= 100 THEN OUTPUT "Valid score" ENDIF
5.
IF Month >= 1 AND _____ <= 12 THEN OUTPUT "Valid month" ENDIF

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A IF Age >= 0 OR Age <= 120
B THEN
C OUTPUT "Valid"
D ENDIF
Question 2
A INPUT Mark
B IF Mark >= 0 AND Mark <= 100
C THEN OUTPUT "Rejected"
D ELSE OUTPUT "Rejected"
E ENDIF
Question 3
A INPUT Temp
B IF Temp < -10 OR Temp > 40
C THEN OUTPUT "Outside range"
D ELSE OUTPUT "Outside range"
E ENDIF
Question 4
A INPUT Rating
B IF Rating > 1 AND Rating <= 5
C THEN OUTPUT "Accepted"
D ENDIF
Question 5
A OUTPUT "Enter mark 0-100"
B INPUT Mark
C WHILE Mark < 0 AND Mark > 100
D INPUT Mark
E ENDWHILE

Answer Key

Range check
Section A: Quick Questions
Q1
A — IF Age >= 0 AND Age <= 120
Q2
A — The value lies between a lowest and a highest allowed value
Q3
B — Yes, 100 is accepted because <= includes the limit
Q4
A — Mark < 0 OR Mark > 100
Q5
B — IF Day >= 1 AND Day < 31
Section B: Match the Pairs
Q1
IF Mark >= 0 AND Mark <= 100 THEN → Range check: accepts a Mark from 0 to 100, including 0 and 100 themselves
Q2
IF Age < 0 OR Age > 120 THEN → Tests whether Age is outside the allowed range, so the value is rejected
Q3
A range check on a date: the month number must be between 1 and 12, and 1 and 12 are valid → IF Month >= 1 AND Month <= 12 THEN
Q4
Both limits must be true for a number to pass, so the two tests are joined with AND → IF Score >= 1 AND Score <= 10 THEN
Q5
IF Age >= 0 OR Age <= 120 THEN → OR is used to join the limits, so every number passes, because any number is either at least 0 or at most 120. It should be AND.
Q6
IF Mark > 0 AND Mark < 100 THEN → The limits 0 and 100 are wrongly rejected because > and < are used. It should be >= and <= so the limits are accepted.
Section C: Definitions
Q1
IF Age >= 0 AND Age <= 120 — A range check tests that a value lies between the lowest and highest allowed values. The limits themselves are accepted. AND joins both limits.
Section D: Fill in the Blanks
Q1
IF Age >= 0 AND Age <= 120 THEN OUTPUT "Age accepted" ENDIF
Q2
IF Age >= 0 AND Age <= 120 THEN OUTPUT "Age accepted" ENDIF
Q3
IF Age < 0 OR Age > 120 THEN OUTPUT "Invalid age" ENDIF
Q4
IF Score >= 0 AND Score <= 100 THEN OUTPUT "Valid score" ENDIF
Q5
IF Month >= 1 AND Month <= 12 THEN OUTPUT "Valid month" ENDIF
Section E: Spot the Error
Q1
A — A range check needs AND so the value must meet both limits. With OR, every number passes, even -5 or 500. It should be IF Age >= 0 AND Age <= 120.
Q2
C — The THEN branch runs when the mark is inside the range, so it should output "Accepted". Here, valid marks would be rejected.
Q3
D — The condition tests for being outside the range, so ELSE means the value is inside it. It should output "In range", not "Outside range".
Q4
B — Values on the limits must be accepted. Using > 1 rejects a rating of 1, which is the lowest allowed value. It should be Rating >= 1.
Q5
C — A number cannot be below 0 and above 100 at the same time, so the loop would never run. To catch values outside the range, use OR: WHILE Mark < 0 OR Mark > 100.