DoAssignment study guide

C2.1 · Use coding to represent algebraic concepts

Learn to use coding to represent algebraic concepts through clear examples and targeted practice.

Ontario Grade 9 Mathematics

Algebra

Ontario MTH1W – Strand C: Coding and Algebra (Expectation C2.1)

You already know that algebra uses letters like xx or nn to stand for unknown or changing values. Coding does the exact same thing — it just uses a slightly different look. In this lesson you will see how every key idea in algebra (variables, expressions, and equations) maps directly onto lines of code. By the end, reading a short program and reading an algebraic expression will feel like two ways of saying the same thing.

What you will learn

  • Explain how a variable in algebra and a variable in code represent the same idea: a named container whose value can change.
  • Write and trace simple pseudocode or Python-style code that stores values, evaluates algebraic expressions, and updates variables.
  • Connect the steps of evaluating an algebraic expression to the matching lines of code.
  • Identify and correct common errors when translating between algebraic notation and code.

Prerequisite Review: Variables and Expressions in Algebra

A variable is a letter that holds a number whose value can change or is not yet known. For example, in the expression 3x+53x + 5, the letter xx is the variable. The numbers 33 and 55 are constants — they stay the same no matter what.
An algebraic expression is a combination of variables, constants, and operations (addition, subtraction, multiplication, division). Evaluating an expression means replacing the variable with a specific number and then calculating the result. For example, if x=4x = 4, then 3x+5=3(4)+5=12+5=173x + 5 = 3(4) + 5 = 12 + 5 = 17.
An equation is a statement that two expressions are equal, such as y=3x+5y = 3x + 5. Here, yy depends on the value of xx. When xx changes, yy changes too. Keep these three ideas — variable, expression, equation — in mind as you move into coding.
y=3x+5y = 3x + 5
  • A variable holds a value that can change.
  • An expression combines variables and constants using operations.
  • Evaluating means substituting a number for the variable and simplifying.
  • An equation links two expressions with an equals sign.

What Is a Variable in Code?

In code, a variable works exactly like an algebraic variable — it is a named storage spot that holds a value. The line of code that creates a variable and gives it a value is called an assignment statement. In Python-style code (used throughout this lesson), an assignment looks like this: x = 4. This means 'store the value 4 in the variable named x.'
Notice that the equals sign in code is an assignment, not a mathematical statement of equality. It is an instruction: 'take the value on the right and put it into the box named on the left.' This is slightly different from the algebraic equals sign, which says two things are already equal. Keeping this distinction clear prevents many common errors.
You can also reassign a variable. If you write x = 4 on one line and then x = 10 on the next line, the variable x now holds 10. The old value 4 is gone. This mirrors what happens in algebra when you substitute a different value for xx to evaluate an expression again.
x←4x \leftarrow 4
  • A variable in code is a named container for a value.
  • An assignment statement stores a value into a variable using =.
  • The = in code means 'store this value here,' not 'these two things are equal.'
  • Reassigning a variable replaces its old value with a new one.

Translating Algebraic Expressions into Code

Every algebraic operation has a direct coding equivalent. Addition, subtraction, and division look almost identical. Multiplication uses the asterisk symbol * instead of a dot or brackets, and exponentiation (powers) uses in Python-style code. Apart from those two symbol differences, the logic is the same.
Suppose you want to code the expression 3x+53x + 5 for x=4x = 4. You first store the value of xx, then write a second line that calculates the expression and stores the result in a new variable. The code would look like this: x = 4 on line 1, and result = 3 * x + 5 on line 2. The computer reads line 2, replaces x with 4, computes 3 × 4 + 5 = 17, and stores 17 in result. That is identical to algebraic substitution.
Order of operations in code follows the same rules as in algebra: multiplication and division are done before addition and subtraction, and brackets are calculated first. So the code line result = 3 * x + 5 multiplies 3 by x before adding 5, just as 3x+53x + 5 does in algebra. If you want addition done first, you must use brackets in both algebra and code: result = 3 * (x + 5).
You can also write code that mirrors an equation with two variables. To code y=3x+5y = 3x + 5, you write x = 4 on one line and y = 3 * x + 5 on the next. The variable y now stores the value that algebra would call the output of the equation for that input.
y=3∗x+5y = 3 * x + 5
  • Use * for multiplication and for exponentiation in Python-style code.
  • The order of operations (BEDMAS) is the same in code and algebra.
  • Use brackets in code whenever you need to change the default order of operations.
  • Two-variable equations translate to two lines of code: one for the input, one for the output.

Using a Loop to Evaluate an Expression for Many Values

One powerful thing code can do is repeat a task automatically. A for loop in code repeats a block of instructions for each value in a list. This is especially useful in algebra when you want to evaluate an expression for several values of a variable and see the pattern.
Consider the algebraic equation y=2x+1y = 2x + 1. To evaluate it for x=1,2,3,4x = 1, 2, 3, 4, you would substitute each value by hand. In code, a for loop does all of those substitutions automatically. The loop says: 'for each value of x in this list, calculate y and show both values.' Each time through the loop, x takes the next value from the list, y is recalculated, and the results are printed.
The connection to algebra is direct: the loop is simply doing repeated substitution. Every pass through the loop is one row in a table of values. If you built a table of values for y=2x+1y = 2x + 1 in your algebra work, a for loop builds that exact same table — the computer just does the arithmetic for you.
Tracing a loop means reading through the code step by step, keeping track of the current value of each variable, exactly as you would evaluate an expression step by step in algebra. This skill of tracing code is the coding version of checking your algebraic work.
y=2∗x+1y = 2 * x + 1
  • A for loop repeats a block of code for each value in a list.
  • Each pass through a loop corresponds to one substitution in an algebraic expression.
  • A loop that evaluates an equation for several x-values produces the same result as a table of values.
  • Tracing code step by step is the coding equivalent of checking algebraic substitution.

Updating a Variable: The Coding Version of Solving Step by Step

In algebra, solving an equation often involves changing the value of an expression step by step — adding a number to both sides, or multiplying through. In code, you can model this step-by-step process by updating a variable. An update line looks like: total = total + 3. This tells the computer to take the current value of total, add 3 to it, and store the new value back into total.
At first glance, total = total + 3 looks like an impossible algebraic equation — you cannot have the same letter on both sides and add 3 to get the same letter. But remember: in code, = means assignment, not equality. The right side is evaluated first using the old value of total, and then the result is stored in total. So if total was 7, after this line total becomes 10.
This kind of update is useful when your code needs to keep a running total, count steps, or model a pattern that grows by a fixed amount each time — all ideas that connect directly to algebraic sequences and linear relationships. Recognising that total = total + 3 models repeated addition of 3 links the code update directly to the algebraic idea of a constant rate of change.
total=total+3\text{total} = \text{total} + 3
  • An update statement changes a variable's value based on its own current value.
  • The right side of an update is evaluated first; then the result is stored in the variable.
  • total = total + 3 is valid code but would be an impossible algebra equation — the = means different things.
  • Repeated updates of the same amount model a constant rate of change, a core linear algebra idea.

Algebra Notation vs. Code Notation — Side-by-Side

Algebra ideaAlgebra notationPython-style code
Store a value in a variablex=4x = 4x = 4
Multiply a variable by a constant3x3x3 * x
Expression with two operations3x+53x + 53 * x + 5
Two-variable equationy=3x+5y = 3x + 5y = 3 * x + 5
Square a variablex2x^{2}x 2
Update (add a constant to a variable)Substitute next valuetotal = total + 3

Tracing a Loop for y = 2x + 1 (x from 1 to 4)

Loop passValue of xCalculationValue of y
1st12(1)+12(1) + 13
2nd22(2)+12(2) + 15
3rd32(3)+12(3) + 17
4th42(4)+12(4) + 19

Worked example

Tracing Code That Evaluates a Two-Variable Equation

A student writes the following Python-style code. Trace through it line by line, state the value stored in each variable after each line runs, and connect each line to the matching algebraic step.

Line 1: x = 6
Line 2: y = 4 * x - 3
Line 3: x = 10
Line 4: y = 4 * x - 3
  1. Identify the algebraic equation in the code
    Lines 2 and 4 both calculate y using the expression 4 * x - 3. In algebra this is the equation y=4x−3y = 4x - 3. The variable x is the input and y is the output.
    y=4x−3y = 4x - 3
  2. Trace Line 1
    The assignment x = 6 stores the value 6 in the variable x. This is equivalent to choosing the input value x=6x = 6 before substituting into the algebraic equation.
    x=6x = 6
  3. Trace Line 2 — first evaluation
    The computer replaces x with its current value, 6, then calculates the expression on the right: 4 times 6 minus 3. Multiplication comes before subtraction (order of operations), so it computes 24 − 3 = 21. The value 21 is stored in y. This mirrors algebraic substitution.
    y=4(6)−3=24−3=21y = 4(6) - 3 = 24 - 3 = 21
  4. Trace Line 3
    The assignment x = 10 replaces the old value 6 with the new value 10. The variable x now holds 10. The variable y has not changed yet — it still holds 21 from Line 2.
    x=10x = 10
  5. Trace Line 4 — second evaluation
    The computer again evaluates 4 * x - 3, but now x = 10. It calculates 4 times 10 minus 3: 40 − 3 = 37. The new value 37 is stored in y, replacing the old value of 21.
    y=4(10)−3=40−3=37y = 4(10) - 3 = 40 - 3 = 37
  6. Summarise the connection to algebra
    The code evaluated the equation y=4x−3y = 4x - 3 twice: once for the input x=6x = 6 giving y=21y = 21, and once for x=10x = 10 giving y=37y = 37. Each pair of lines (input assignment then expression calculation) is exactly one row in the algebraic table of values for this equation.
    (6, 21) and (10, 37)
Answer: After Line 1: x=6x = 6. After Line 2: y=21y = 21. After Line 3: x=10x = 10. After Line 4: y=37y = 37. The code evaluates the algebraic equation y=4x−3y = 4x - 3 for two different inputs.
Check: Substitute back into y=4x−3y = 4x - 3: for x=6x = 6, 4(6)−3=24−3=214(6) - 3 = 24 - 3 = 21 ✓. For x=10x = 10, 4(10)−3=40−3=374(10) - 3 = 40 - 3 = 37 ✓. Both values match.

Common mistakes and how to avoid them

Writing x = x + 3 and thinking it is an impossible equation because 'a number cannot equal itself plus 3' — confusing the code assignment operator with the algebraic equals sign.
Correction: In code, = means 'store the result on the right into the variable on the left.' The right side is calculated first using the old value, then the result replaces the old value. It is not an algebraic equation.
Forgetting the * symbol and writing y = 3x + 5 in code, which most languages read as a variable named '3x' — a syntax error.
Correction: Always use * for multiplication in code: write y = 3 * x + 5. The algebraic shorthand of writing a number directly next to a letter does not work in code.
Assuming that when x is reassigned, y automatically updates too — for example, thinking that after x = 10, y still equals the new result without running the y = ... line again.
Correction: In code, y only updates when its assignment line runs. Changing x does not automatically change y. You must run y = 3 * x + 5 again after the new x value is set.
Ignoring order of operations in code and thinking result = 3 * x + 5 is the same as result = 3 * (x + 5).
Correction: Code follows BEDMAS just like algebra. Multiplication is done before addition, so 3 * x + 5 computes (3 × x) + 5, not 3 × (x + 5). Use brackets whenever you need to override the default order.
Thinking a for loop changes the algebraic meaning of the expression — believing the loop does something different from substitution.
Correction: A for loop simply repeats substitution for each value in the list. Each pass through the loop is identical to one manual substitution in algebra. The loop does not change the expression; it just automates the arithmetic.

Lesson summary

  • A variable in code is a named container for a value, just like a variable in algebra.
  • An assignment statement uses = to store a value; this is different from the algebraic equals sign, which states that two things are equal.
  • Algebraic expressions translate directly into code: use * for multiplication and for powers; order of operations (BEDMAS) applies in both.
  • A for loop performs repeated substitution automatically, producing the same results as a table of values for an algebraic equation.
  • Updating a variable with a line like total = total + 3 models a constant rate of change and is valid code even though it would be an impossible algebraic equation.
  • Tracing code line by line — tracking each variable's current value — is the coding version of checking algebraic substitution step by step.

Check your understanding

Question 1

A student writes this code: x = 5 and then y = 2 * x + 7. What value is stored in y after both lines run?
  1. 12
  2. 17
  3. 24
  4. 35
Show answer and explanation
17
Substitute x = 5 into the expression: 2(5)+7=10+7=172(5) + 7 = 10 + 7 = 17. Multiplication is done before addition, following order of operations. The answer is 17.

Question 2

In Python-style code, what does the line total = total + 4 do if total currently holds the value 9?
  1. It causes an error because a number cannot equal itself plus 4.
  2. It stores the value 13 into total, replacing 9.
  3. It stores the value 4 into total, ignoring the old value.
  4. It stores the value 9 into a new variable called total + 4.
Show answer and explanation
It stores the value 13 into total, replacing 9.
In code, = means assignment, not algebraic equality. The right side is calculated first: 9+4=139 + 4 = 13. Then 13 is stored back into total, replacing 9. The result is total = 13.

Question 3

A for loop evaluates y = 3 * x - 1 for x = 1, 2, 3. Which set of y-values does the loop produce, in order?
  1. 2, 5, 8
  2. 3, 6, 9
  3. 4, 7, 10
  4. 1, 4, 7
Show answer and explanation
2, 5, 8
Substitute each value: for x=1x=1, 3(1)−1=23(1)-1=2; for x=2x=2, 3(2)−1=53(2)-1=5; for x=3x=3, 3(3)−1=83(3)-1=8. The loop produces 2, 5, 8.

Question 4

A student wants to code the algebraic expression 4(x+2)4(x + 2). Which line of code is correct?
  1. result = 4 * x + 2
  2. result = 4 * x + 2 * 4
  3. result = 4 * (x + 2)
  4. result = 4x + 2
Show answer and explanation
result = 4 * (x + 2)
The brackets in 4(x+2)4(x+2) mean addition is done before multiplication. In code, brackets work the same way: 4 * (x + 2) computes x + 2 first, then multiplies by 4. Without brackets, 4 * x + 2 would compute 4x+24x + 2 instead, which is a different expression.

Key terms

Variable (algebra)
A letter that represents a value that can change or is not yet known, such as xx or nn.
Variable (code)
A named container in a program that stores a value. It works the same way as an algebraic variable.
Assignment statement
A line of code that stores a value into a variable using the = symbol, for example x = 4.
Expression
A combination of variables, constants, and operations that represents a value, such as 3x+53x + 5.
Evaluate
To substitute a specific number for a variable and calculate the result.
For loop
A coding structure that repeats a block of instructions for each value in a given list.
Trace
To read through code line by line, tracking the current value of each variable at every step.
Update statement
A line of code that changes a variable's value based on its current value, such as total = total + 3.

Continue through MTH1W

View the complete MTH1W Ontario Grade 9 Mathematics curriculum and lessons

Use the free MTH1W test and exam review guide

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 9 Mathematics (MTH1W), expectation C2.1. It is a study resource, not an official curriculum publication.

Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.

Official curriculum reference

Report a correction or ask a question