Expression Evaluation (Cambridge (CIE) A Level Computer Science): Revision Note

Exam code: 9618

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Reverse Polish Notation (RPN)

What is RPN?

  • Reverse Polish Notation (RPN) is a method of writing mathematical expressions where the operator comes after the operands

  • This is also known as postfix notation

    • Infix: 3 + 4

    • RPN: 3 4 +

Why use RPN?

  • No need for brackets: The order of operations is determined by position, not parentheses

  • Easier for computers to evaluate using a stack

  • Always unambiguous

How RPN works

  • RPN uses a stack to evaluate expressions

  1. Read from left to right

  2. Push numbers onto the stack

  3. When an operator is read:

    • Pop the top two values from the stack

    • Apply the operator

    • Push the result back onto the stack

  4. When the expression ends, the final result is at the top of the stack

Example: evaluate 3 4 + 2 ×

Step

Action

Stack

Read 3

Push to stack

3

Read 4

Push to stack

3 4

Read +

Add 3 + 4 → 7

7

Read 2

Push to stack

7 2

Read ×

Multiply 7 × 2 → 14

14

  • Final result: 14

Example: convert this infix expression to RPN

(7 − 2 + 8) / (9 − 5)
  • Split the expression into parts:

  • Left-hand side:

(7 − 2 + 8)

  • This becomes:

( (7 − 2) + 8 )

  • Which in RPN is:

7 2 − 8 +

  • Right-hand side:

(9 − 5)

  • In RPN:

9 5 −

  • Combine with the division

  • In RPN, that becomes:

7 2 − 8 + 9 5 − ÷

Examiner Tips and Tricks

RPN follows the evaluation order naturally, so there’s no need for brackets or precedence rules. Just:

  • Work from inside brackets out

  • Convert each sub-expression

  • Add the operator after its operands

Summary

Concept

Description

RPN (Postfix)

Operators follow operands

Stack

Used to store and evaluate values

Evaluation method

Push numbers, pop and evaluate on operator

Benefits

No brackets, no ambiguity, easy to compute with stack

You've read 0 of your 5 free revision notes this week

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.