Skip to content
ExamSpine

Lesson 1.9

Operators and precedence

Part 1: Programs that talkReading, about 4 min3 exercises in this lesson, in Python2 exercises in this lesson, in Java

Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.

Operators and the order they run in

By the end of this you will know the arithmetic operators and be able to work out what a line computes when more than one operator appears.

The operators

OperatorWhat it doesExampleResult
+add3 + 47
-subtract10 - 37
*multiply5 * 210
/divide7 / 23.5
%remainder7 % 21

In Python, ** raises to a power (2 ** 3 is 8). In Java, use Math.pow(2, 3) instead.

Division with / gives a decimal in Python. In Java, / between two ints gives a whole number (the next lesson covers this). The remainder % gives what is left after dividing: 7 % 2 is 1 because 7 is 3 twos with 1 left over.

Order of operations

When a line has more than one operator, multiplication and division happen before addition and subtraction, the same rule as in maths. Brackets change the order.

a = 2 + 3 * 4
b = (2 + 3) * 4
print(a)
print(b)
public static void main(String[] args) {
    int a = 2 + 3 * 4;
    int b = (2 + 3) * 4;
    System.out.println(a);
    System.out.println(b);
}

When it runs:

14
20

Without brackets, 3 * 4 is computed first (giving 12), then 2 + 12 gives 14. With brackets, 2 + 3 is computed first (giving 5), then 5 * 4 gives 20.

When in doubt, add brackets. They make the order clear to anyone reading your code, and they never change a result you already got right.

A common mistake: writing a + b / 2 when you mean (a + b) / 2. The first divides only b by 2, then adds a.

Now try it yourself on the questions below.

Exercise 1.9.1 · Trace itTrace: precedence

Complete the trace table for this Python code. [2]

Line 1: a = 10
Line 2: b = a - 3 * 2
Line 3: a = a + b
Line 4: print(a)
Line 5: print(b)

Fill in one row each time a line changes a or b, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.

Rowaboutput
1
2
3
4
5

Exercise 1.9.21.9.1 · Write itTicket cost

Write a program that reads the price of one ticket (a whole number) on the first line and the number of tickets (a whole number) on the second line, and prints the total cost on one line.

  1. The total is the price times the number of tickets, plus a fixed booking fee of 5.
  2. The fee is added once, not once per ticket: total = price times count, plus 5.
  3. Print only the total, as a number. Spaces at the end are ignored.

Your program is run several times. Only what it prints is checked.

In Python, use int to convert each line. In Java, use Integer.parseInt.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • price times count plus the fee (rules 1 and 2)

    You type

    1. typed: 10
    2. typed: 3

    It prints

    35
  • one ticket (rules 1 and 2)

    You type

    1. typed: 10
    2. typed: 1

    It prints

    15
solution.pySolution.java

Exercise 1.9.31.9.2 · Write itRectangle

Write a program that reads the width and height of a rectangle, each a whole number on its own line, and prints two lines:

Area: 15
Perimeter: 16
  1. The first line is Area: and one space, then the width times the height.
  2. The second line is Perimeter: and one space, then twice the sum of width and height.
  3. The width is typed first, then the height.
  4. Print nothing else. Spaces at the end of a line are ignored.

Your program is run several times. Only what it prints is checked.

In Python, use int to convert each line. In Java, use Integer.parseInt.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • a 5 by 3 rectangle (rules 1 to 4)

    You type

    1. typed: 5
    2. typed: 3

    It prints

    Area: 15
    Perimeter: 16
  • a 1 by 1 square (rules 1 and 2)

    You type

    1. typed: 1
    2. typed: 1

    It prints

    Area: 1
    Perimeter: 4
solution.pySolution.java