01 / 21
x = 42

Variables &
Assignments

Learn how computers store, update, and use data through variables: the fundamental building blocks of every program.

AP Computer Science Principles

What Is a Variable?

A variable is a named container in a computer's memory that holds a value. Think of it like a labeled box: the label is the variable's name, and the contents are its current value.

score 95 name "Ada" passed true Each box stores one value, accessed by its name

Why Do Variables Matter?

Without variables, a program would have no way to remember information. Every calculation would be lost the moment it was computed. Variables let programs:

Store Data
Save a user's input, a sensor reading, or a computed result for later use.
Update State
Track changing values like a game score, countdown timer, or account balance.
Reuse Values
Reference the same value in multiple places without recomputing it every time.

Common Data Types

Every value stored in a variable has a type that determines what operations you can perform on it.

Integer
Whole numbers like 7, -3, 0. Used for counting and indexing.
String
Text in quotes like "hello". Used for names, messages, and labels.
Boolean
Only true or false. Used for decisions and conditions.
Float
Decimal numbers like 3.14, -0.5. Used for precise measurements.

The Assignment Operator

In programming, the assignment operator stores a value into a variable. The AP CSP exam uses the arrow notation:

score ← 95 AP CSP PSEUDOCODE score = 95 PYTHON / JAVASCRIPT

Assignment vs. Equality

In math, \( x = 5 \) means "x equals 5, always." In programming, \( x \leftarrow 5 \) means "right now, put 5 into x." The value of x can change later.

Math Equality Programming Assignment
Symbol \( = \) \( \leftarrow \) (or =)
Meaning "is equal to" "store this value"
Direction Symmetric (both sides) Right to left only
Can change? No (permanent truth) Yes (value can be overwritten)

How Assignment Works Step by Step

1
Evaluate the right side. Compute the expression on the right side of \( \leftarrow \) first. If it references other variables, use their current values.
2
Store into the left side. Take the computed result and place it into the variable named on the left. If the variable already had a value, the old value is overwritten.
3
Continue to the next line. The program moves forward. The variable now holds its new value until it is changed again.

Tracing Variables: Your Checklist

When the AP exam asks "What is the value of x after this code runs?", follow this tracing technique:

1
Draw a table. Create a column for each variable mentioned in the code.
2
Process line by line. Execute each assignment statement in order, top to bottom.
3
Evaluate right side first. Use the current values of variables, not future ones.
4
Update the left side. Cross out the old value and write the new one in your table.
5
Read the final row. After all lines execute, the last value in each column is the answer.

Example 1: Simple Assignment

$$ \begin{align*} \text{x} & \leftarrow 10 \\ \text{y} & \leftarrow 3 \\ \text{z} & \leftarrow \text{x} + \text{y} \end{align*} $$
TRACE TABLE x y z Line 1 Line 2 Line 3 10 -- -- 10 3 -- 10 3 13 Final: x = 10, y = 3, z = 13

Example 2: Overwriting Values

$$ \begin{align*} \text{a} & \leftarrow 5 \\ \text{b} & \leftarrow \text{a} \times 2 \\ \text{a} & \leftarrow 20 \end{align*} $$
TRACE TABLE a b Line 1 Line 2 Line 3 5 -- 5 10 20 10

Example 3: The Swap Problem

How do you swap the values of two variables? You need a temporary variable:

$$ \begin{align*} \text{x} & \leftarrow \text{"left"} \\ \text{y} & \leftarrow \text{"right"} \\ & \text{\# Swap x and y:} \\ \text{temp} & \leftarrow \text{x} \\ \text{x} & \leftarrow \text{y} \\ \text{y} & \leftarrow \text{temp} \end{align*} $$
TRACE TABLE x y temp L1 L2 L4 L5 L6 "left" -- -- "left" "right" -- "left" "right" "left" "right" "right" "left" "right" "left" "left"

Why Temp Is Essential

What happens if you try to swap without a temporary variable?

$$ \begin{align*} & \text{\# BROKEN swap (data is lost!)} \\ \text{x} & \leftarrow \text{"left"} \\ \text{y} & \leftarrow \text{"right"} \\ \text{x} & \leftarrow \text{y} \quad \text{\# x is now "right"} \\ \text{y} & \leftarrow \text{x} \quad \text{\# y is now "right" too!} \end{align*} $$

Expressions on the Right Side

The right side of an assignment can be any expression: a calculation involving numbers, variables, and operators that produces a single value.

$$ \begin{align*} \text{price} & \leftarrow 25 \\ \text{tax} & \leftarrow \text{price} \times 0.08 \\ \text{total} & \leftarrow \text{price} + \text{tax} \\ \text{count} & \leftarrow \text{count} + 1 \end{align*} $$
Arithmetic
Use +, -, *, /, and MOD (remainder) to compute values.
Self-Reference
\( \text{count} \leftarrow \text{count} + 1 \) is valid: it reads the old value, adds 1, and stores the new result.

Strings and Concatenation

Variables can also store text (strings). You can combine strings using concatenation: joining them end-to-end.

$$ \begin{align*} \text{first} & \leftarrow \text{"Ada"} \\ \text{last} & \leftarrow \text{"Lovelace"} \\ \text{full} & \leftarrow \text{first} + \text{" "} + \text{last} \\ & \text{\# full is now "Ada Lovelace"} \end{align*} $$

Common Mistakes

✕ Reading Left to Right
Students read \( x \leftarrow 5 \) as "x goes into 5." The arrow means the opposite: the value flows from right to left.
✕ Thinking Updates Are Linked
After \( b \leftarrow a \), changing a does not change b. The value was copied, not linked.
✕ Swapping Without Temp
Trying \( x \leftarrow y \) then \( y \leftarrow x \) loses the original value of x. Always use a temp variable.

Summary: Key Concepts

Concept What It Means Example
Variable Named container for data score, name
Assignment Store a value (right \( \rightarrow \) left) \( x \leftarrow 10 \)
Overwrite New value replaces old \( x \leftarrow 20 \)
Expression Computed value on right side \( z \leftarrow x + y \)
Swap Exchange two values via temp 3 assignments needed

Question 1 of 5

What is the value of y after the following code executes?

$$ \begin{align*} \text{x} & \leftarrow 4 \\ \text{y} & \leftarrow \text{x} + 6 \\ \text{x} & \leftarrow 100 \end{align*} $$

Question 2 of 5

What is the value of a after this code runs?

$$ \begin{align*} \text{a} & \leftarrow 3 \\ \text{a} & \leftarrow \text{a} \times \text{a} \end{align*} $$

Question 3 of 5

Which of the following correctly swaps the values of variables p and q?

Question 4 of 5

What does the following display?

$$ \begin{align*} \text{x} & \leftarrow \text{"5"} \\ \text{y} & \leftarrow \text{"3"} \\ \text{DISPLAY}(\text{x} + \text{y}) \end{align*} $$

Question 5 of 5

After the following code, what are the values of m and n?

$$ \begin{align*} \text{m} & \leftarrow 1 \\ \text{n} & \leftarrow 2 \\ \text{m} & \leftarrow \text{m} + \text{n} \\ \text{n} & \leftarrow \text{m} + \text{n} \end{align*} $$