Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Programming Fundamentals · Grade 6-8

Assignment

The operation of storing a value in a variable.


Quick Answer

The operation of storing a value in a variable. You use assignment every time a program needs to remember something: a score, a name, a running total. In score = score + 10, the computer works out the right side first using the current score, then puts the result into the variable on the left, replacing what was there. The = sign is a command to store, not a statement that two things are equal; in most languages that question is asked with ==.

Why This Matters

Every changing value in a program, from a game score to a bank balance, changes through assignment. Once you read = as 'store the right side in the name on the left', you can trace any program line by line and predict its output. Read it as equality from algebra and count = count + 1 looks impossible, and swaps and running totals come out wrong.

Intuitive Explanation

Picture a row of labelled boxes. The line x = 5 means: find the box labelled x and put 5 inside. If the box already held 3, the 3 is thrown out, because a box holds one value at a time. The line y = x does not tie y to x forever; it looks in x right now, copies the 5 and puts the copy in y. Change x later and y still holds 5.

The order of work is always right side first, then store. Take score = 40 followed by score = score + 10. The computer reads the right side with the old value, 40 + 10 = 50, and only then writes 50 into score. A line that is nonsense in algebra, where no number equals itself plus 10, makes perfect sense in code: the two mentions of score belong to different moments, before and after the update.

Two things look like assignment and are not. In a math equation, x = 5 and 5 = x say the same thing; in code, 5 = x is an error, because you cannot store anything in the number 5. And the test x == 5 stores nothing at all: it asks whether x currently holds 5 and answers true or false.

Think of it like this. Each variable is a small whiteboard with its name written at the top. Assigning means erasing the board and writing the new value, so it only ever shows the latest one, and copying from one board to another copies what is written right now. The picture holds for numbers and text; for lists, many languages copy a link to the same list instead.

Core idea. Assignment is not equality — it's an action. x = x + 1 makes no sense in math, but in programming it means 'update x.'


When to Use

Assignment is the move whenever a program must store, update or swap a value: setting a starting score, adding to a running total, counting tries, saving a user's name. Trace it line by line wherever a single = has a name on its left. If a line asks whether two values are the same, it is a comparison (==), not an assignment. If the question is what kind of value is stored, a whole number, text or true/false, that is about Integer, String and Boolean.

Tip. Ask: Does a line store a value into a name with a single =, working out the right side before the left side changes?

How to Recognize It

Look at what each line does to a name: changes what it holds, only reads it, or only asks a question about it.

  1. Is there a name on the left of a single = with a value or a calculation on the right?

    Yes means the line stores something, so it is assignment. If the name only appears inside a calculation or a print, the line just reads a Variable.

  2. Does the same name appear on both sides, as in total = total + price?

    Yes means an update: work out the right side with the old total, then replace it. Do not treat it like one of the Equations from math class, because nothing is being solved.

  3. Is the line inside an if or while condition, using == to compare two values?

    Yes means nothing is stored: the comparison produces a Boolean, true or false, and the variable keeps its value. A single = in that spot is usually a bug.

  4. Do you need to know what a name holds after several lines have run?

    Yes means trace each assignment in order and write down the new value every time a line stores one. If no line stores into that name, you are only reading a Variable.

Assignment vs Variable vs Equations vs Boolean

Code and math both put names next to an equals sign, so these blur together. Ask what the line does to the name: changes what it holds, simply is the named box, claims two sides are equal, or asks whether they are.

Assignment

Meaning
Assignment is the action that works out a value and stores it in a named variable, replacing what was there.
Key test
The line changes what a name holds; the value moves from right to left.
Example
lives = lives - 1 takes the current 3, subtracts 1 and stores 2 back in lives.

Variable

Meaning
A variable is the named storage place itself, which keeps its value until an assignment replaces it.
Key test
It is the box, not the action; print(score) reads it without changing it.
Example
In print(score + 5), score is read and 5 is added for display, but score keeps its old value.

Equations

Meaning
An equation in math states that two expressions are equal, and solving it finds the value that makes this true.
Key test
Nothing is stored; you find x. In math, x = x + 1 has no solution.
Formula
2x+3=11
Example
2x + 3 = 11 is solved to get x = 4, and no computer stores anything.

Boolean

Meaning
A Boolean is a true or false value, and a comparison such as x == 5 produces one without changing x.
Key test
A double == asks a question; the answer is true or false and x stays the same.
Example
With x = 3, the comparison x == 5 gives False, and x is still 3 afterwards.

Formula & Notation

Worked Examples

Example 1 — Trace four assignments

Easy

Trace this program and give the final values of a and b: a = 4, then b = a + 3, then a = b * 2, then b = b - a.
Show the steps and the answer
  1. a = 4: a holds 4.

    The first line stores a starting value; b does not exist yet.

  2. b = a + 3: the right side is 4 + 3 = 7, so b holds 7.

    The right side uses whatever a holds at this moment, which is 4.

  3. a = b * 2: the right side is 7 × 2 = 14, so a now holds 14 and the old 4 is gone.

    Assignment overwrites a; b keeps its 7 because this line stores nothing in b.

  4. b = b - a: the right side is 7 - 14 = -7, so b holds -7.

    Both names on the right are read before anything is stored, using b = 7 and the new a = 14.

Answer: a = 14 and b = -7.

Takeaway: Keep a small table of current values: each line reads from the table, then changes exactly one entry.

Example 2 — Why a swap needs a spare box

Standard

Two variables hold x = 3 and y = 8. To swap them, a student writes x = y and then y = x. What do x and y hold afterwards, and how do you swap correctly?
Show the steps and the answer
  1. x = y: x becomes 8, and the 3 is overwritten.

    Assignment does not keep a copy of the old value anywhere.

  2. y = x: x already holds 8, so y becomes 8. Result: x = 8, y = 8, and the 3 is lost.

    The right side reads x after the first line changed it.

  3. Swap with a temporary variable: temp = x (3), then x = y (8), then y = temp (3).

    Saving the old x before overwriting it keeps both values alive.

  4. In Python, x, y = y, x also works: the right side becomes the pair (8, 3) before either name is stored.

    This is the right-side-first rule again: everything on the right is read using the old values.

Answer: The two-line attempt leaves x = 8 and y = 8. With temp = x, x = y, y = temp you get x = 8 and y = 3.

Takeaway: Assignment replaces; it does not exchange. Save any value you will need again before you overwrite it.

Example 3 — Find the mistake: an update is not an equation

Application

Each ticket in a game costs 5 coins. The program runs count = 3, then count = count + 2, then cost = count * 5. Leo says: "count = count + 2 can't be true, so the computer skips it, and cost is 15." Find the mistake and correct it.
Show the steps and the answer
  1. Read count = count + 2 as an instruction: compute count + 2, then store the result in count.

    In code = means store, so the line is not a claim that can be true or false, and it always runs.

  2. Evaluate the right side with the current value: 3 + 2 = 5, then store 5 in count.

    The count on the right is the old value, 3; the count on the left receives the new one.

  3. Compute cost = count * 5 = 5 × 5 = 25.

    This line comes after the update, so it reads 5, not 3.

  4. Check against the story: 3 tickets plus 2 more is 5 tickets, at 5 coins each, 25 coins.

    The traced value matches what the program is meant to describe.

Answer: count becomes 5, so cost = 25 coins, not 15. The line count = count + 2 runs and updates count.

Takeaway: A line like count = count + 2 is an update: read the old value, change it, store it back.

Where You'll See It

  • Updating your game score

    When you earn 10 points, the code runs score = score + 10 — it reads the old score, adds 10, and stores the new total back in score; that is assignment as an action, not a math equation.

  • Putting a label on a storage box

    name = 'Mia' is like writing 'Mia' on a slip and dropping it in a box labeled name — the variable is the labeled box and the value is what you put inside, and you can swap the contents later.

  • A condition that accidentally assigns instead of compares

    Writing if (x = 5) sets x to 5 instead of checking if it equals 5 — mixing up = (assign) and == (compare) is a classic bug that this concept warns about.

Common Mistakes

  1. The slip: x = x + 1 is impossible because a number cannot equal itself plus one.

    Why it's tempting: In math = means 'is equal to,' and no number equals itself plus one, so it looks like a contradiction.

    Instead: In code = is an instruction, not a claim of equality. x = x + 1 means 'compute x + 1, then store that as the new x' — it updates the variable.

  2. The slip: The variables on the right side use their NEW values during the assignment.

    Why it's tempting: Since the left variable is changing, students think the change happens before the right side is read.

    Instead: The right side is fully evaluated FIRST using the current (old) values, and only then is the result stored on the left. So in x = x + 1, the x on the right is the old x.

  3. The slip: Writing if (score = 100) to check whether score is 100.

    Why it's tempting: The condition asks whether score equals 100, and a single = is the sign you have always written for 'equals'.

    Instead: A test needs ==, which compares and gives true or false: if (score == 100). A single = stores 100 in score instead; Python rejects it as a syntax error, and in C or JavaScript it overwrites score and the test passes even when score was 40.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. Trace this code: x = 4; x = x * 3; x = x - 2. What is the final value of x?

    Hint: Work out the right side using the current x, then store the result.

  2. Trace: a = 5; b = 9; a = b; b = a. What are a and b at the end?

    Hint: After the third line, what value does a hold? The fourth line copies that.

  3. Trace: a = 5; b = 9; temp = a; a = b; b = temp. What are a, b and temp at the end?

    Hint: temp saves a copy of a before a is overwritten.

  4. Trace: x = 2; y = x + 3; x = 10. What is y at the end?

    Hint: When was the value of y calculated?

  5. Look at two lines: (a) score = 10 and (b) if score == 10: print('Top score'). Which line is an assignment, and what does the other one do?

    Hint: One line stores a value. The other asks a question.

  6. A student says: 'x = x + 1 is impossible, because no number equals itself plus 1.' Explain what the line really does when x is 6.

    Hint: Read = as 'store' rather than 'equals'.

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Can one line assign to several variables at once?

Yes, in Python. x = y = 0 stores 0 in both names, and a, b = 1, 2 stores 1 in a and 2 in b, matching values to names by position. JavaScript also accepts x = y = 0, and it unpacks with square brackets, as in [a, b] = [1, 2]. Each name still gets its own number, so changing x afterwards leaves y at 0.

What happens if I use a variable before assigning to it?

It depends on the language, and none of the results is what you want. Python stops with NameError, because the name does not exist until an assignment creates it. JavaScript gives undefined for a variable declared with let but not yet given a value, and C may hand you whatever leftover number happens to sit in that memory. Giving every variable a starting value, such as total = 0 before a loop, avoids all three.

Why do some languages write x := 5 or x←5 instead of x = 5?

Those symbols stop anyone mistaking storing for equality. Pascal uses :=, Go uses := to create a new variable, and many textbooks and exam boards write the arrow in pseudocode, showing the value moving into x. Python added := in 2019 for assigning inside a condition. The meaning matches = in Python, Java or JavaScript: work out the right side, then store it.

What does score += 10 mean?

It is a shortcut for score = score + 10: take the current score, add 10 and store the result back in score. Most languages have the same shortcut for other operations, such as lives -= 1 and price *= 2, and Java and JavaScript also have count++, which adds 1. The rule does not change: the old value is read first and then replaced, so score must already hold a value before the line runs.

Learning Path

← Before

Variable
Assignment

You are here

You need Variable (a named place that holds a value) first. Assignment adds the action that puts a value into that place, with the right side worked out before it is stored. Next, Integer, String and Boolean show the kinds of values you can assign and what each kind lets you do: arithmetic, joining text, and making decisions.

See Also