Expressions & Assignment (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Expressions to assign values

How do you use expressions to assign values?

  • An expression combines variables, values, and arithmetic operators to produce a single value

  • An assignment stores that value in a variable so it can be used later in a program

  • In SQA pseudocode, assignments are written using the format:

SET <variable> TO <expression>

  • This means that the expression on the right-hand side is evaluated first, and the result is stored in the variable on the left-hand side

Examples

SQA pseudocode example

Python equivalent

Description

SET area TO length * width

area = length * width

Calculates the area of a rectangle and stores it in area

SET total TO price + tax

total = price + tax

Adds the price and tax together and stores the result in total

SET average TO (mark1 + mark2 + mark3) / 3

average = (mark1 + mark2 + mark3) / 3

Calculates the average of three marks and stores it in average

SET remainder TO number MOD 2

remainder = number % 2

Stores the remainder when number is divided by 2 in remainder

SET totalMarks TO totalMarks + score

totalMarks = totalMarks + score

Updates a running total by adding the latest score to the existing totalMarks

  • An example SQA-style pseudocode program could be:

DECLARE price INITIALLY 0
DECLARE quantity INITIALLY 0
DECLARE total INITIALLY 0

SEND "Enter the price:" TO DISPLAY
RECEIVE price FROM KEYBOARD

SEND "Enter the quantity:" TO DISPLAY
RECEIVE quantity FROM KEYBOARD

SET total TO price * quantity
SEND "Total cost is " & total TO DISPLAY

Expressions to return values

How do you use expressions to return values?

  • An expression that returns a value produces a result which can be used immediately

  • For example, in an output statement or when passing data into another expression

  • You should be able to:

    • Create expressions using arithmetic operators

    • Combine variables, values, and built-in functions

    • Use the result directly in an output, comparison, or further calculation

  • In SQA pseudocode, arithmetic operations follow the same syntax as for assignment, but the expression is not stored, it simply produces a result

Examples

SQA pseudocode example

Python equivalent

Description

SEND 10 * 5 TO DISPLAY

print(10 * 5)

Calculates and immediately displays the result of multiplying 10 by 5

SEND (a + b) / 2 TO DISPLAY

print((a + b) / 2)

Outputs the average of two values without storing it

IF (number MOD 2) = 0 THEN

if number % 2 == 0:

Uses the result of an arithmetic expression to check if a number is even

SEND ROUND(price * 1.2) TO DISPLAY

print(round(price * 1.2))

Calculates and displays the rounded total after a 20% increase

SEND "Total: " & (cost + tax) TO DISPLAY

print("Total:", cost + tax)

Combines text with a calculated expression to display the total cost

  • An example SQA-style pseudocode program could be:

DECLARE number1 INITIALLY 0
DECLARE number2 INITIALLY 0

SEND "Enter first number:" TO DISPLAY
RECEIVE number1 FROM KEYBOARD

SEND "Enter second number:" TO DISPLAY
RECEIVE number2 FROM KEYBOARD

SEND "The sum of the two numbers is " & (number1 + number2) TO DISPLAY
SEND "The average of the two numbers is " & ((number1 + number2) / 2) TO DISPLAY
SEND "The larger number divided by the smaller is " & (number1 / number2) TO DISPLAY

Arithmetic operations

What is an arithmetic operation?

  • Arithmetic operators are used in programs to perform mathematical calculations such as addition, subtraction, multiplication, and division

Operation

SQA pseudocode

Python equivalent

Description

Addition

+

+

Adds two numbers

Subtraction

-

-

Subtracts one number from another

Multiplication

*

*

Multiplies two numbers

Division

/

/

Divides one number by another

Modulus (remainder after division)

MOD

%

Returns the remainder of a division

Quotient (whole number division)

DIV

//

Returns the whole number result of a division

Exponentiation (to the power of)

^

**

Raises a number to the power of another

  • To demonstrate the use of common arithmetic operators, three sample programs written in SQA pseudocode are given below

Arithmetic operators #1

SQA Pseudocode

Python equivalent

SEND "Enter a number:" TO DISPLAY
RECEIVE number FROM KEYBOARD

IF number MOD 2 = 0 THEN
    SEND "The number is even." TO DISPLAY
ELSE
    SEND "The number is odd." TO DISPLAY
END IF
user_input = int(input("Enter a number: "))

if user_input % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")
  • The program divides the number by 2 using MOD

  • If the remainder is 0, the number is even

Arithmetic operators #2

SQA Pseudocode

Python equivalent

SEND "Enter the radius of the circle:" TO DISPLAY
RECEIVE radius FROM KEYBOARD

SET area TO 3.14159 * (radius ^ 2)

SEND "The area of the circle is " & area TO DISPLAY
radius = float(input("Enter the radius of the circle: "))

area = 3.14159 * radius ** 2

print("The area of the circle with radius", radius, "is", area)
  • Uses ^ for exponentiation to square the radius and calculates the area using the formula π × r²

Arithmetic operators #3

SQA Pseudocode

Python equivalent

DECLARE score INITIALLY 0

REPEAT 5 TIMES
    SEND "Enter the first number:" TO DISPLAY
    RECEIVE num1 FROM KEYBOARD

    SEND "Enter the operator (+, -, *):" TO DISPLAY
    RECEIVE operator FROM KEYBOARD

    SEND "Enter the second number:" TO DISPLAY
    RECEIVE num2 FROM KEYBOARD

    IF operator = "+" THEN
        SET correctAnswer TO num1 + num2
    ELSE
        IF operator = "-" THEN
            SET correctAnswer TO num1 - num2
        ELSE
            IF operator = "*" THEN
                SET correctAnswer TO num1 * num2
            ELSE
                SEND "Invalid operator!" TO DISPLAY
            END IF
        END IF
    END IF

    SEND "What is " & num1 & " " & operator & " " & num2 & "?" TO DISPLAY
    RECEIVE userAnswer FROM KEYBOARD

    IF userAnswer = correctAnswer THEN
        SET score TO score + 1
    ELSE
        SEND "Sorry, that is incorrect." TO DISPLAY
    END IF
END REPEAT

SEND "Your score is " & score TO DISPLAY
score = 0

for x in range(5):
    num1 = int(input("Enter the first number: "))
    operator = input("Enter the operator (+, -, *): ")
    num2 = int(input("Enter the second number: "))
    user_answer = int(input("What is " + str(num1) + " " + str(operator) + " " + str(num2) + "? "))

    if operator == '+':
        correct_answer = num1 + num2
    elif operator == '-':
        correct_answer = num1 - num2
    elif operator == '*':
        correct_answer = num1 * num2
    else:
        print("Invalid operator!")
        continue

    if user_answer == correct_answer:
        score = score + 1
    else:
        print("Sorry, that's incorrect.")

print("Your score is:", score)
  • The program uses a fixed loop (REPEAT 5 TIMES) and arithmetic operators +, -, and * to create and check simple maths questions

Worked Example

The total cost should be output as shown below.

The total is £8575.00 for the doors

Using a programming language of your choice and the variable name totalCost, write the code to produce the output above.

[2]

Answer

  • Display the correct text using quotes [1 mark]

  • totalCost variable used between text with appropriate separators [1 mark]

Example answers can include:

SEND "The total is £" & totalCost & " for the doors" TO DISPLAY

print("The total is £" + str(totalCost) + " for the doors")

Unlock more, it's free!

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

the (exam) results speak for themselves:

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.