Comparison operators

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

Comparison operators

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which operator means 'not equal to' in Cambridge pseudocode?
A !=
B <>
2. Mark ← 50. What does the condition Mark >= 50 give?
A TRUE
B FALSE
3. Mark ← 50. What does the condition Mark > 50 give?
A FALSE
B TRUE
4. Which line correctly tests whether Age is equal to 16 in a condition?
A IF Age == 16 THEN
B IF Age = 16 THEN
5. What type of value does a comparison such as Score < 40 give?
A TRUE or FALSE
B A number

Match the Pairs

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

Left columnRight column
1Score <> 0 AIF Password = "abc123" THEN
2Age >= 16 BTRUE when Age is 16 or more, including exactly 16
3A single = in a condition tests whether two values are equal and gives TRUE or FALSE CIF Mark > Max THEN
4> is strictly greater than, so a value equal to the limit gives FALSE DTRUE when Score is not equal to 0, FALSE when Score is 0
5IF Count => 10 THEN EThe operator is written the wrong way round. It should be >= (greater than or equal to), not =>.
6IF Number != 5 THEN F!= is not a pseudocode operator. 'Not equal to' is written <>.

Definitions

=, <>, <, >, <=, >=

Fill in the Blanks

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

1.
IF Password _ "abc123" THEN OUTPUT "Welcome" ENDIF
2.
IF Answer __ "yes" THEN OUTPUT "Try again" ENDIF
3.
IF Age __ 16 THEN OUTPUT "Allowed" ENDIF
4.
WHILE Lives _ 0 DO Lives ← Lives - 1 ENDWHILE
5.
Mark ← 39 Fail ← Mark __ 39 OUTPUT Fail

Spot the Error

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

Question 1
A IF Age => 18 THEN
B OUTPUT "Adult"
C ENDIF
Question 2
A Score ← 50
B Pass ← 40
C IF Score <= Pass THEN
D OUTPUT "Pass"
E ENDIF
Question 3
A INPUT Guess
B IF Guess == 7 THEN
C OUTPUT "Correct"
D ENDIF
Question 4
A INPUT Age
B Vote ← FALSE
C IF Age > 18 THEN
D Vote ← TRUE
E ENDIF
Question 5
A INPUT Name
B IF Name = Ava THEN
C OUTPUT "Hello Ava"
D ENDIF

Answer Key

Comparison operators
Section A: Quick Questions
Q1
B — <>
Q2
A — TRUE
Q3
A — FALSE
Q4
B — IF Age = 16 THEN
Q5
A — TRUE or FALSE
Section B: Match the Pairs
Q1
Score <> 0 → TRUE when Score is not equal to 0, FALSE when Score is 0
Q2
Age >= 16 → TRUE when Age is 16 or more, including exactly 16
Q3
A single = in a condition tests whether two values are equal and gives TRUE or FALSE → IF Password = "abc123" THEN
Q4
> is strictly greater than, so a value equal to the limit gives FALSE → IF Mark > Max THEN
Q5
IF Count => 10 THEN → The operator is written the wrong way round. It should be >= (greater than or equal to), not =>.
Q6
IF Number != 5 THEN → != is not a pseudocode operator. 'Not equal to' is written <>.
Section C: Definitions
Q1
=, <>, <, >, <=, >= — Comparison operators compare two values and give TRUE or FALSE. = equal, <> not equal, < less than, > greater than, <= less than or equal, >= greater than or equal.
Section D: Fill in the Blanks
Q1
IF Password = "abc123" THEN OUTPUT "Welcome" ENDIF
Q2
IF Answer <> "yes" THEN OUTPUT "Try again" ENDIF
Q3
IF Age >= 16 THEN OUTPUT "Allowed" ENDIF
Q4
WHILE Lives > 0 DO Lives ← Lives - 1 ENDWHILE
Q5
Mark ← 39 Fail ← Mark <= 39 OUTPUT Fail
Section E: Spot the Error
Q1
A — The operator for 'greater than or equal to' is >=, with > first then =. => is not a valid comparison operator. It should be IF Age >= 18 THEN.
Q2
C — A score of 50 with a pass mark of 40 is a pass, so the test must be 'greater than or equal to'. <= is the wrong way round. It should be IF Score >= Pass THEN.
Q3
B — Cambridge pseudocode tests equality with a single =. == is used in some other languages but is not pseudocode. It should be IF Guess = 7 THEN.
Q4
C — A person aged exactly 18 can vote, but > 18 would give FALSE for 18. Use >= to include the boundary value. It should be IF Age >= 18 THEN.
Q5
B — Text being compared must be written in double quotes. Without them, Ava looks like a variable name. It should be IF Name = "Ava" THEN.