Expressions & Assignment (SQA National 5 Computing Science): Revision Note
Exam code: X816 75
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 |
|---|---|---|
|
| Calculates the area of a rectangle and stores it in |
|
| Adds the price and tax together and stores the result in |
|
| Calculates the average of three marks and stores it in |
|
| Stores the remainder when |
|
| Updates a running total by adding the latest |
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 DISPLAYExpressions 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 |
|---|---|---|
|
| Calculates and immediately displays the result of multiplying 10 by 5 |
|
| Outputs the average of two values without storing it |
|
| Uses the result of an arithmetic expression to check if a number is even |
|
| Calculates and displays the rounded total after a 20% increase |
|
| 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 DISPLAYArithmetic 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) |
|
| Returns the remainder of a division |
Quotient (whole number division) |
|
| 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 |
|---|---|
| |
The program divides the number by 2 using
MODIf the remainder is 0, the number is even
Arithmetic operators #2
SQA Pseudocode | Python equivalent |
|---|---|
| |
Uses
^for exponentiation to square the radius and calculates the area using the formula π × r²
Arithmetic operators #3
SQA Pseudocode | Python equivalent |
|---|---|
| |
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]
totalCostvariable 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!
Did this page help you?