Error: wrong relational operator

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

Error: wrong relational operator

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A cinema lets in anyone aged 18 or over. Which condition is correct?
A IF Age > 18 THEN
B IF Age >= 18 THEN
2. The algorithm uses IF Age > 18 THEN to allow adults in. Which input value shows the error?
A Age = 18, the boundary value
B Age = 40, a typical value
3. A pass mark is 50 or more. Which line has a logic error?
A IF Mark > 50 THEN
B IF Mark >= 50 THEN
4. A bus fare discount is for people aged 16 or under. Which condition is correct?
A IF Age <= 16 THEN
B IF Age < 16 THEN
5. Which statement about the error Age > 18 instead of Age >= 18 is true?
A It is a syntax error: the program will not run
B It is a logic error: the program runs but gives the wrong result for 18

Match the Pairs

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

Left columnRight column
1IF Age > 18 THEN A> should be >=. A person aged exactly 18 is wrongly rejected, because 18 > 18 is false. Test with the boundary value 18 and correct the operator to >=.
2IF Age >= 18 THEN BIF Age <= 12 THEN
3To test a rule 'a pass mark is 50 or more', the boundary value 50 must be accepted, so use the operator that includes it. C< should be <=. A Score of exactly 40 is wrongly missed, because 40 < 40 is false. Test with the boundary value 40 and correct the operator to <=.
4A rule says 'a child ticket is for ages up to and including 12'. The boundary value 12 must be included, so the operator needs an equals part. DIF Mark >= 50 THEN
5IF Score < 40 THEN OUTPUT "Fail" ENDIF (Rule: 40 or less fails) ECorrect operator for 'aged 18 or over': the boundary value 18 is accepted as well as everything above it.
6IF Age > 18 THEN OUTPUT "Can vote" ENDIF (Rule: 18 or over can vote) FWrong at the boundary: a person aged exactly 18 is rejected, even though the rule says 18 or over should be accepted.

Definitions

Age >= 18

Fill in the Blanks

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

1.
IF Age __ 18 THEN OUTPUT "Adult" ENDIF
2.
IF Temp __ 0 THEN OUTPUT "Freezing: 0 counts" ENDIF
3.
// Adults are 18 or over IF Age >= __ THEN OUTPUT "Adult"
4.
Count ← 1 WHILE Count __ 10 DO Count ← Count + 1
5.
IF Mark _ 40 THEN OUTPUT "Fail: 40 is a pass" ENDIF

Spot the Error

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

Question 1
A INPUT Age
B IF Age > 18 THEN
C OUTPUT "Can vote"
D ENDIF
Question 2
A DECLARE Mark : INTEGER
B OUTPUT "Enter mark"
C INPUT Mark
D IF Mark > 50 THEN
E OUTPUT "Pass"
F ENDIF
Question 3
A IF Age < 12 THEN
B Fare ← 5
C ENDIF
Question 4
A Count ← 0
B INPUT Score
C // 80 or more earns a merit
D IF Score > 80 THEN
E Count ← Count + 1
F ENDIF
Question 5
A IF Temp > 30 THEN
B OUTPUT "Hot day"
C ENDIF

Answer Key

Error: wrong relational operator
Section A: Quick Questions
Q1
B — IF Age >= 18 THEN
Q2
A — Age = 18, the boundary value
Q3
A — IF Mark > 50 THEN
Q4
A — IF Age <= 16 THEN
Q5
B — It is a logic error: the program runs but gives the wrong result for 18
Section B: Match the Pairs
Q1
IF Age > 18 THEN → Wrong at the boundary: a person aged exactly 18 is rejected, even though the rule says 18 or over should be accepted.
Q2
IF Age >= 18 THEN → Correct operator for 'aged 18 or over': the boundary value 18 is accepted as well as everything above it.
Q3
To test a rule 'a pass mark is 50 or more', the boundary value 50 must be accepted, so use the operator that includes it. → IF Mark >= 50 THEN
Q4
A rule says 'a child ticket is for ages up to and including 12'. The boundary value 12 must be included, so the operator needs an equals part. → IF Age <= 12 THEN
Q5
IF Score < 40 THEN OUTPUT "Fail" ENDIF (Rule: 40 or less fails) → < should be <=. A Score of exactly 40 is wrongly missed, because 40 < 40 is false. Test with the boundary value 40 and correct the operator to <=.
Q6
IF Age > 18 THEN OUTPUT "Can vote" ENDIF (Rule: 18 or over can vote) → > should be >=. A person aged exactly 18 is wrongly rejected, because 18 > 18 is false. Test with the boundary value 18 and correct the operator to >=.
Section C: Definitions
Q1
Age >= 18 — Using > instead of >= (or < instead of <=) is a logic error: the boundary value, such as Age 18, is wrongly rejected. Test the boundary value, then correct the operator.
Section D: Fill in the Blanks
Q1
IF Age >= 18 THEN OUTPUT "Adult" ENDIF
Q2
IF Temp <= 0 THEN OUTPUT "Freezing: 0 counts" ENDIF
Q3
// Adults are 18 or over IF Age >= 18 THEN OUTPUT "Adult"
Q4
Count ← 1 WHILE Count <= 10 DO Count ← Count + 1
Q5
IF Mark < 40 THEN OUTPUT "Fail: 40 is a pass" ENDIF
Section E: Spot the Error
Q1
B — Using > instead of >= wrongly rejects someone aged exactly 18, the boundary value. It should be IF Age >= 18 THEN.
Q2
D — A mark of exactly 50 should pass, but > rejects it. Testing the boundary value 50 shows the error. It should be IF Mark >= 50 THEN.
Q3
A — A 12 year old should get the child fare, but < excludes the boundary value 12. It should be IF Age <= 12 THEN.
Q4
D — A score of exactly 80 earns a merit, but > does not count it. The operator should be >=, so it should be IF Score >= 80 THEN.
Q5
A — A temperature of exactly 30 should count as a hot day. With > the boundary value 30 is missed. It should be IF Temp >= 30 THEN.