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
easyAnswer
First step
Full solution
- 2 Step 2: y is assigned the current value of x, which is 10. So y = 10.
- 3 Step 3: x is reassigned to 20. y is unchanged (still 10). Output: x = 20, y = 10.
Example 2
easyExample 3
mediumExample 4
hardPractice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
easyExample 2
mediumExample 3
easyExample 4
easyExample 5
easyExample 6
easyExample 7
easyExample 8
easyExample 9
easyExample 10
easyExample 11
mediumExample 12
mediumExample 13
mediumExample 14
mediumExample 15
mediumExample 16
mediumExample 17
mediumExample 18
mediumExample 19
mediumExample 20
challengeExample 21
challengeExample 22
challengeExample 23
easyExample 24
easyExample 25
easyExample 26
easyExample 27
easyExample 28
easyExample 29
mediumExample 30
mediumExample 31
mediumExample 32
mediumExample 33
mediumExample 34
mediumExample 35
mediumExample 36
mediumExample 37
mediumExample 38
mediumExample 39
mediumExample 40
hardExample 41
hardExample 42
hardExample 43
hardExample 44
challengeBackground Knowledge
These ideas may be useful before you work through the harder examples.