Assignment Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Assignment.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

The operation of storing a value in a variable. The variable name goes on the left of the assignment operator, and the value or expression goes on the right. The right side is evaluated first, then the result is stored in the variable on the left.

Assignment is like putting a label on a box and putting something inside. The label is the variable name; the contents is the value.

Read the full concept explanation โ†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

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.'

Common stuck point: = means 'assign,' not 'equals.' Many languages use == for equality comparison.

Sense of Study hint: When you see x = x + 1, read it as a two-step process: first compute the right side (take the current value of x and add 1), then store the result back into x on the left side. The right side is always evaluated before the assignment happens.

Worked Examples

Example 1

easy
Trace the following code: SET x = 10. SET y = x. SET x = 20. OUTPUT x, y.

Answer

x = 20, y = 10.

First step

1
Step 1: x is assigned 10.

Full solution

  1. 2
    Step 2: y is assigned the current value of x, which is 10. So y = 10.
  2. 3
    Step 3: x is reassigned to 20. y is unchanged (still 10). Output: x = 20, y = 10.
Assignment copies the current value, not a reference. Changing x later does not affect y because y received the value (10), not a link to x.

Example 2

easy
What is the difference between `x = 5` (assignment) and `x == 5` (comparison)?

Example 3

medium
Trace: `x = 1`, `y = 10`, `x += y`, `y -= x`. Final `x`, `y`?

Example 4

hard
Trace: `i = 0`, `total = 0`. Repeat while `i < 4`: `i = i + 1`; `total = total + i`. Final `i`, `total`?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

easy
What is the output? SET a = 3. SET b = 4. SET a = a + b. SET b = a - b. OUTPUT a, b.

Example 2

medium
Trace the values of x and y after each line: SET x = 10 SET y = x + 5 SET x = y * 2 SET y = x - y What are the final values of x and y?

Example 3

easy
After `x = 5`, what is the value of `x`?

Example 4

easy
Trace: `a = 3`, then `a = 7`. What is `a` at the end?

Example 5

easy
Is `=` the assignment operator or the equality test?

Example 6

easy
Trace: `x = 2`, `y = x`. What is `y`?

Example 7

easy
After `n = 4`, evaluate `n = n + 1`. What is `n`?

Example 8

easy
Trace: `s = 1`, `s = s * 3`. What is `s`?

Example 9

easy
After `flag = True`, what value is stored in `flag`?

Example 10

easy
Trace: `a = 10`, `b = a - 4`. What is `b`?

Example 11

medium
Trace: `x = 1`, `y = 2`, `x = y`, `y = x`. What are `x` and `y`?

Example 12

medium
Swap with a temp: `a = 5`, `b = 8`, `t = a`, `a = b`, `b = t`. Final `a`, `b`?

Example 13

medium
Trace: `total = 0`, then `total = total + 2`, `total = total + 3`. What is `total`?

Example 14

medium
Trace: `p = 2`, `q = p + 1`, `p = q * 2`. What are `p` and `q`?

Example 15

medium
In `if count = 5:` a beginner used one `=`. What is wrong and what is the fix?

Example 16

medium
Trace: `x = 4`, `x = x // 2`, `x = x // 2`. What is `x`?

Example 17

medium
Trace: `name = "A"`, `name = name + "B"`, `name = name + "C"`. What is `name`?

Example 18

medium
Trace: `a = 1`, `b = a`, `a = 9`. What is `b` after all three lines?

Example 19

medium
Trace: `x = 5`, `y = 0`, `y += x`, `x += y`. Final `x`, `y`? (`+=` adds in place)

Example 20

challenge
Trace without a temp swap attempt using arithmetic: `a = 6`, `b = 4`, `a = a + b`, `b = a - b`, `a = a - b`. Final `a`, `b`?

Example 21

challenge
Trace: `x = 2`. Repeat `x = x * x` three times. What is `x`?

Example 22

challenge
Given `a = 1`, `b = 2`, `c = 3`, then `a = b`, `b = c`, `c = a`. Final `a`, `b`, `c`?

Example 23

easy
After `x = 8`, what is the value of `x`?

Example 24

easy
Trace: `a = 4`, then `a = 11`. What is `a` at the end?

Example 25

easy
Trace: `x = 6`, `y = x`. What is `y`?

Example 26

easy
After `n = 9`, evaluate `n = n - 2`. What is `n`?

Example 27

easy
Trace: `s = 4`, `s = s * 5`. What is `s`?

Example 28

easy
After `flag = False`, what value is stored in `flag`?

Example 29

medium
Trace: `x = 3`, `y = 5`, `x = x + y`, `y = x - y`. Final `x`, `y`?

Example 30

medium
Swap with a temp: `a = 7`, `b = 2`, `t = a`, `a = b`, `b = t`. Final `a`, `b`?

Example 31

medium
Trace: `total = 0`, `total = total + 5`, `total = total + 7`, `total = total + 3`. What is `total`?

Example 32

medium
Trace: `p = 5`, `q = p + 2`, `p = q * 3`. What are `p` and `q`?

Example 33

medium
In `if total = 100:` a beginner used one `=`. What does this typically do or fail to do?

Example 34

medium
Trace: `x = 16`, `x = x // 2`, `x = x // 2`, `x = x // 2`. What is `x`? (`//` is integer division)

Example 35

medium
Trace: `s = "go"`, `s = s + "od"`, `s = s + "!"`. What is `s`?

Example 36

medium
Trace: `a = 3`, `b = a`, `a = 12`. What is `b` after all three lines?

Example 37

medium
Trace: `x = 6`, `x *= 2`, `x -= 3`. What is `x`?

Example 38

medium
Trace: `count = 0`. Run `count = count + 1` five times. What is `count`?

Example 39

medium
Trace: `a = 0`, `b = 1`. Do `t = a + b`; `a = b`; `b = t`. Repeat twice from the result. Final `a`, `b`?

Example 40

hard
Trace: `x = 2`. Run `x = x * x` four times. What is `x`?

Example 41

hard
Given `a = 2`, `b = 5`, `c = 8`, run `a = c`, `c = b`, `b = a`. Final `a`, `b`, `c`?

Example 42

hard
Trace arithmetic swap: `a = 9`, `b = 2`, `a = a + b`, `b = a - b`, `a = a - b`. Final `a`, `b`?

Example 43

hard
Trace: `x = 3`, `y = x + 2`, `x = y * y`, `y = x - y`. Final `x`, `y`?

Example 44

challenge
Trace: `a = 1`, `b = 1`. Repeat 5 times: `t = a + b`, `a = b`, `b = t`. Final `b`?

Background Knowledge

These ideas may be useful before you work through the harder examples.

variable