GGCSE Revision Hub

3.2.4 Relational operations in a programming language

AQA Computer Science 8525Topic 3.2 — ProgrammingFoundation & Higher

This is the longest topic in the specification and the one that carries most of the Paper 1 marks. It covers the statements every program is built from, the operators that do the work, the structures that hold data, and the discipline that makes code safe to rely on. Two of the specification's supported languages are Python and C#, so the examples here use Python-style syntax with the AQA pseudo-code where the specification demands it. Learn this as something you write, not something you memorise.

What you need to know

The six operators

Be familiar with and be able to use equal to, not equal to, less than, greater than, less than or equal to, and greater than or equal to. These are the operators that compare two values, and their result is a Boolean, which is exactly what a selection or iteration condition needs.

Key fact: Six relational operators, each producing a Boolean.

Reading them in code

In Python they are written = = (with a space, because a single = is assignment), ≠ or ! = , < , > , <= and >=. In AQA pseudo-code they are often written as = , <> , < , > , <= and >=. The specification warns that different languages may use different symbols to represent these operators, so the safest answer interprets the operator rather than reciting one symbol.

Key fact: Read the operator, not the symbol — different languages write them differently.

Equal to and assignment

The single most common error in programming is writing = where you mean ==. A single equals sign assigns a value: score = 0. Two equals signs compare. If a question asks what a line of code does, check very carefully whether that symbol is storing something or testing something, because the two lines look almost identical and do completely different things.

Key fact: A single = assigns; a double = compares. Check which one you have.

Using them in conditions

Students should be able to use these operators within their own programs and be able to interpret them when used within algorithms. In an IF condition the relational operator decides which branch runs, and in a WHILE condition it decides whether the loop continues. So in a trace table, the relational operator is often the single cell that tells you what happens next.

Key fact: Relational operators decide which branch runs and whether a loop continues.

Chaining comparisons carefully

A common exam trap is a condition that looks as if it says a value is between two others. In Python, `1 < x < 10` does test that properly, but in most other languages and in pseudo-code it does not mean what it looks like. Write two separate tests with AND if you want a range check, and check what the question's language actually does.

Key fact: To test a range in most languages, use two comparisons joined with AND.

Comparing the right types

Relational operators need compatible types. Comparing a string with an integer is either an error or meaningless depending on the language, and comparing the string '10' with the integer 10 gives a surprising answer. If one side of a comparison is user input, it must be converted to the right type first — which is exactly what the string conversion operations in 3.2.8 are for.

Key fact: Only compare compatible types; convert user input before comparing it.

Flashcards

Exam questions

Written in the style of AQA Computer Science, with a mark scheme for each one. No question here is a real past paper question.

mcq1 mark

What does the condition `age >= 18` test?

  • That age is less than 18
  • That age is exactly 18
  • That age is 18 or more
  • That age is not 18

Mark scheme: 1 mark for identifying that >= is greater than or equal to, so the condition is true when age is 18 or more.

short3 marks

Explain in English why a single = sign and a double == sign cause very different behaviour in a program, and what happens if you use the wrong one in an IF condition.

Mark scheme: 1 mark for stating that a single = assigns a value to a variable while a double == compares two values. 1 mark for explaining that using = in a condition stores the value and the condition then tests what that value is, so the branch may always run. 1 mark for explaining the consequence, that the program may behave incorrectly or throw an error rather than making the intended comparison.

six6 marks

Explain in English the six relational operators and how they are used in programs.

Mark scheme: Level 1 (1-2 marks): A short answer naming two or three of the operators. Level 2 (3-4 marks): A structured answer covering all six and the fact that each produces a Boolean. Level 3 (5-6 marks): A detailed answer listing all six operators and noting that symbols differ between languages, explaining that the result is a Boolean used by selection and iteration conditions, distinguishing assignment from comparison, explaining how to test a range correctly with two comparisons joined by AND, and explaining that both sides of a comparison must be of compatible types so user input has to be converted first.

Other sections in this topic

Practise these questions

Answer them in the interactive view and they get marked against the same mark scheme, one mark at a time, with the reason for each mark.

Open the interactive topic →