Arithmetic, comparison and Boolean operators

GCSE Computer Science (OCR) · OCR J277 · Algorithms and programming (Paper 2) › Programming fundamentals › Programming fundamentals

66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.

Or take a quick quiz: 11 questions on this subtopic in GCSE Computer Science (OCR), marked as you tap.

Revision notes

Operators let a program calculate, compare and combine conditions. You need the arithmetic operators (including MOD, DIV and ^), the comparison operators and the Boolean operators AND, OR and NOT. Expect 'evaluate this expression' questions and conditions inside code you write.

Key facts

  • + − * / : add, subtract, multiply, divide (/ can give a decimal)
  • MOD = remainder: 17 MOD 5 = 2
  • DIV = whole-number division: 17 DIV 5 = 3
  • ^ = power: 3 ^ 2 = 9
  • == equal, != not equal, < <= > >= compare values (result True or False)
  • AND: both true; OR: at least one true; NOT: reverses
  • Even number test: num MOD 2 == 0
  • Inside a range: AND; outside a range: OR

Notes

Arithmetic operators

diagram
  • + add, - subtract, * multiply, / divide (7 / 2 = 3.5).
  • MOD gives the remainder of a division: 17 MOD 5 = 2.
  • DIV gives the whole-number part (quotient) of a division: 17 DIV 5 = 3.
  • 17 counters shared into groups of 517 DIV 5 = 3 (3 full groups)17 MOD 5 = 2 (2 left over)
    DIV counts the full groups; MOD is what is left over
  • ^ raises to a power (exponent): 2 ^ 3 = 8.
  • Brackets are worked out first, then powers, then * / MOD DIV, then + and −. Use brackets to make the order clear.
  • Uses: num MOD 2 == 0 is true for even numbers; 135 seconds is 135 DIV 60 = 2 minutes and 135 MOD 60 = 15 seconds.

Comparison operators

  • == equal to, != not equal to, < less than, <= less than or equal to, > greater than, >= greater than or equal to.
  • A comparison always gives a Boolean result: True or False.
  • Check boundaries carefully: age > 18 is false when age is 18; age >= 18 is true.

Boolean operators

diagram
  • AND: True only if both conditions are True.
  • OR: True if at least one condition is True.
  • NOT: reverses the result, so NOT True is False.
  • A range check needs AND: score >= 0 AND score <= 100. Outside a range needs OR: score < 0 OR score > 100.
  • score >= 0 AND score <= 1000100score < 0 OR score > 1000100
    Inside a range: AND. Outside a range: OR
  • Each side of AND or OR must be a full comparison: write x == 1 OR x == 2, not x == 1 OR 2.

How to answer each type of question

Evaluate expressions

1 mark eachGrade 5
  1. Do brackets first, then powers, then * / MOD DIV, then + and −.
  2. For MOD, find the remainder; for DIV, the whole number of times it goes in.
  3. For a Boolean expression, work out each comparison as True or False first, then apply AND, OR or NOT.

Example. State the result of each expression.
(a) 23 MOD 4 (b) 23 DIV 4 (c) 3 ^ 2 + 1 (d) (10 > 3) AND (4 == 5) [4]

Show the model answerHide the model answer
(a) 3
(1)
(b) 5
(1)
(c) 10
(1)
(d) False (1): 10 > 3 is True but 4 == 5 is False, and AND needs both.

Write a condition

2 marksGrade 6
  1. Decide which comparisons are needed, and whether the limits are included (use <= or >=).
  2. Join them with AND if all must be true, or OR if any one is enough.
  3. Write each comparison in full.

Example. Write a condition that is True when age is from 13 to 19 inclusive and member is True. [2]

Show the model answerHide the model answer
age >= 13 AND age <= 19 AND member == true
Correct range 13 to 19 inclusive using AND (1); joined with AND to member being True
(1).

Use MOD and DIV in a program

3 marksGrade 7
  1. Use DIV to find how many whole units fit.
  2. Use MOD to find what is left over.
  3. Cast numbers with str() when you join them into a message.

Example. Write code that inputs a number of seconds and outputs it in minutes and seconds. For example, 200 should output 3 minutes 20 seconds. [3]

Show the model answerHide the model answer
secs = int(input("Enter seconds"))
mins = secs DIV 60
left = secs MOD 60
print(str(mins) + " minutes " + str(left) + " seconds")
Uses DIV 60 to find the minutes (1); uses MOD 60 to find the seconds left over (1); outputs both values in a message (1).

Don’t lose marks

  • Swapping MOD and DIV.
  • Using > when the question says 'or more' (that needs >=).
  • Writing x == 1 OR 2 instead of x == 1 OR x == 2.
  • Using AND for 'outside a range', which can never be true (a number cannot be below 0 and above 100 at once).
  • Using / when a whole-number answer is needed: 7 / 2 is 3.5, but 7 DIV 2 is 3.

More tips

Memory tricks

  • MOD = 'what's left over' (remainder). DIV = 'how many whole times it goes in'.
  • Check: (a DIV b) × b + (a MOD b) should equal a. For 17 and 5: 3 × 5 + 2 = 17.
  • AND is strict (both), OR is generous (either).

Exam technique

  • Show the working for MOD and DIV (for example 23 = 5 × 4 + 3) so a slip can still earn credit.
  • Words to watch: 'at least' and 'or more' mean >=; 'at most' and 'up to' mean <=; 'more than' means >.
  • In Boolean questions, write True or False for each part before combining them.

What each grade needs

What you need to be able to do, from the first marks up to the top grade.

  1. Grade 3
    Use the basic arithmetic operatorsAdd, subtract, multiply and divide with +, −, * and /.
  2. Grade 4
    Use the comparison operators correctlyUse ==, !=, <, <=, > and >= to compare values and give True or False.
  3. Grade 5
    Evaluate expressions using MOD, DIV and ^17 MOD 5 = 2, 17 DIV 5 = 3 and 2 ^ 3 = 8.
  4. Grade 5
    Combine conditions with AND, OR, NOTWork out whether a combined condition is True or False.
  5. Grade 6
    Write a condition for a rangeFor 13 to 19 inclusive: age >= 13 AND age <= 19.
  6. Grade 7
    Use MOD and DIV to solve problemsTest for even numbers, or split a total into units, such as minutes and seconds.

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

State the value of 17 MOD 5.
2
State the value of 20 MOD 6.
2
State the symbol used in a program for multiplication.
*
Complete the sentences. Choose from: arithmetic, Boolean, comparison, integer, string.
A ................ operator compares two values. The result of the comparison is a ................ value, which is either True or False.
comparison; Boolean

Sample questions

Written for this site in the style of OCR exam questions. They are not taken from real past papers.

Question 1Easy4 marks
(a) State the value of 17 MOD 5.
[1]
(b) State the value of 17 DIV 5.
[1]
(c) State the value of 2 ^ 4.
[1]
(d) Which operator means not equal to?
Tick (✓) one box.
[1]
  • ==
  • !=
  • <=
  • =
Show the answer and mark scheme
(a) Answer: 2
  • 2
(b) Answer: 3
  • 3
(c) Answer: 16
  • 16
(d) Answer: !=
Question 2Medium5 marks
(a) A program should output Even when the integer num is even. Complete the condition in this line of code.
if ...................... then
[2]
(b) A program sets a = 6, b = 10 and c = 3. State whether each expression is True or False.
  • a < b AND c > 5
  • a < b OR c > 5
  • NOT (a == 6)
[3]
Show the answer and mark scheme
(a) Answer: num MOD 2 == 0
  • num MOD 2
  • == 0
(b) Answer: False, True, False
  • False
  • True
  • False
Question 3Hard6 marks
A theme park ride has these rules:
  • every rider must be at least 120 cm tall
  • riders under 14 years old must be with an adult.
A program uses the variables height (integer, in cm), age (integer) and withAdult (Boolean). In this language AND is evaluated before OR.
(a) Write a Boolean expression that is True only when a rider is allowed on the ride.
[3]
(b) State whether a rider with height = 125, age = 12 and withAdult = false is allowed on the ride.
[1]
(c) A programmer writes the expression without brackets:
height >= 120 AND age >= 14 OR withAdult
Give values of height, age and withAdult for which this expression gives the wrong result, and explain why.
[2]
Show the answer and mark scheme
(a) Answer: height >= 120 AND (age >= 14 OR withAdult == true)
  • height >= 120
  • AND (age >= 14 OR withAdult == true)
  • brackets (or equivalent structure) so that the height check applies in every case
(b) Answer: Not allowed (False)
  • not allowed / False
(c)
  • any height below 120 with withAdult true, e.g. 110, 10, true
  • AND is done first, so the expression becomes (height >= 120 AND age >= 14) OR withAdult, which is True for a rider who is too short

More in Programming fundamentals

Stuck? Get 1-to-1 help.

Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.