View as PDF

COMPUTING AND DIGITAL LITERACY

Comparison Operators

Difficulty level: Easy

Computing and Digital Literacy

This document was made by AI and there may be mistakes. If a question or answer looks wrong, use the Flag a mistake button to let Brett know so he can fix it.

Overview of the concept

This is how a program or a spreadsheet decides what to do. It gives a discount only if a total is over fifty dollars, by checking whether one value is bigger, smaller, or equal to another. Comparison operators are the decision makers in code, and almost every rule in an app or a spreadsheet is built from them.

Worked examples

Example 1

Compare two numbers

Solution

5 > 3 -> True

5 < 3 -> False

5 == 5 -> True

5 != 5 -> False

Write the two values with the comparison between them, such as 5 > 3.

Ask the question the symbol asks: is 5 greater than 3? Yes, so True.

Remember == tests equality while a single = stores a value; they are different jobs.

Example 2

Check a pass mark

Solution

score = 62

score >= 50 -> True

So the program prints 'Pass'.

Store the score: score = 62.

Test it against the pass mark with >=, meaning greater than or equal to.

62 >= 50 = True, so the program prints Pass.

Practice questions

1

What does 7 > 4 give?

2

What does 7 < 4 give?

3

What does 7 == 7 give?

4

What does 7 != 7 give?

5

What does 7 >= 7 give?

6

What does 7 <= 6 give?

7

What is the difference between = and ==?

8

Which operator should the test use to check whether a viewer's age is at least 18?

9

Maya scores 45 and the pass mark is 50. Her quiz app tests score >= 50. What does that test give?

10

What type of value does every comparison give?

ANSWER KEY

1. True

2. False

3. True

4. False

5. True

6. False

7. = stores a value; == tests whether two values are equal

8. >=

9. False

10. A boolean: True or False