PrepGo

Variables and Assignments - AP Computer Science Principles Study Guide

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Learn with study guides reviewed by top AP teachers. This guide takes about 14 minutes to read.

Getting Started

To create useful and interactive programs, we need a way to remember and manage information that can change, such as a user's score in a game, the current date, or the total cost of items in a shopping cart. Computers use a concept called a variable to act as a named container for a piece of data. By giving data a name, we can easily reference, update, and use it throughout our programs, forming the foundation of all meaningful computation.

What You Should Be Able to Do

  • Explain the purpose of a variable as an abstraction for a memory location.

  • Use the assignment operator to store a value or the result of an expression in a variable.

  • Evaluate computational expressions that include variables, numbers, and arithmetic operators.

  • Trace the value of one or more variables as they change during the execution of a program.

Key Concepts & Application

The Core Idea

Think of a variable as a labeled box. The label is the variable's name (e.g., playerScore), and the item inside the box is its value (e.g., 100). In programming, a variable is an abstraction for a location in the computer's memory that stores a single value. We don't need to know the physical memory address; we just use the variable's name to interact with the data it holds.

We can perform two primary actions with this "box":

  1. Read the value: We can look inside the box to see what value it currently holds.

  2. Assign a new value: We can take the old item out and put a new one in. This process is called assignment.

This ability to store and update data allows our programs to be dynamic. An algorithm, which is a finite sequence of instructions to accomplish a task, relies heavily on variables to keep track of its progress and manipulate data to solve a problem.

Logic & Application

The core mechanism for using variables is the assignment statement. This statement uses an assignment operator (represented in pseudocode as <-) to place a value into a variable. The value on the right side of the operator is evaluated first, and the result is then stored in the variable on the left.

Key Principles for Naming Variables

  • Be Descriptive: A name like finalScore is much clearer than fs or x. Good names make code readable for you and others.

  • Be Consistent: Use a consistent style, such as camelCase (firstName) or snake_case (first_name).

  • Start with a Letter: Variable names cannot start with a number or contain spaces.

Annotated Pseudocode Examples

1. Basic Assignment

This example shows how to assign a literal numeric value to a variable.


// Declare a variable named 'playerScore' and assign it the value 100.

playerScore <- 100


// Display the current value stored in playerScore.

DISPLAY(playerScore) // Output: 100

2. Assignment with an Expression

The value assigned to a variable can be the result of an expression, which is a combination of values, variables, and operators that evaluates to a single new value.


// Assign initial values to two variables.

itemsInCart <- 5

shippingCost <- 10


// Create an expression using variables and a number.

// The expression (itemsInCart * 2) + shippingCost is evaluated first: (5 * 2) + 10 = 20.

// The result, 20, is then assigned to the totalCost variable.

totalCost <- (itemsInCart * 2) + shippingCost


DISPLAY(totalCost) // Output: 20

3. Updating a Variable's Value

A variable's true power is that its value can change. A common pattern is to update a variable based on its own current value.


// Initialize the score at the start of a game level.

score <- 500

DISPLAY(score) // Output: 500


// The player finds a bonus. The expression 'score + 50' is evaluated (500 + 50 = 550).

// The result, 550, is then assigned back to the 'score' variable, replacing the old value.

score <- score + 50

DISPLAY(score) // Output: 550

Tracing & Analysis

Tracing is the process of tracking the state of variables as a program executes, line by line. This is a critical skill for understanding how algorithms work and for finding errors in code.

Logic Trace

Let's trace the values of the variables a, b, and temp through the following pseudocode snippet.

Pseudocode:


a <- 10

b <- 25

temp <- a

a <- b

b <- temp

Trace Table:

Line Executeda Valueb Valuetemp ValueNotes
(Initial State)(undefined)(undefined)(undefined)Variables have no value yet.
a <- 1010(undefined)(undefined)a is assigned the value 10.
b <- 251025(undefined)b is assigned the value 25.
temp <- a102510temp gets a copy of a's value.
a <- b252510a's value is replaced by b's value.
b <- temp251010b's value is replaced by temp's value.
(Final State)251010The values of a and b have been swapped.

Key Terminology & Logic

A compact reference for the core concepts introduced in this topic.

Term / SymbolDescription
VariableA named reference to a value that can be updated. It is an abstraction for a memory location.
<-The assignment operator. It evaluates the expression on its right and stores the result in the variable on its left.
ExpressionA combination of values, variables, operators, or procedure calls that evaluates to a single value.

Core Concepts & Terminology

  • Variable: An abstraction for a memory location used to store a value. By using a name, programmers can easily reference and manipulate data without needing to know its physical address.

  • Assignment: The action of storing a value in a variable using an assignment operator (<-). This action overwrites any previous value stored in the variable.

  • Expression: A segment of code that produces a value. For example, 5 + 10 is an expression that evaluates to 15, and currentScore + bonus is an expression whose value depends on the current values of those variables.

  • Algorithm: A finite set of instructions that accomplishes a specific task. Algorithms use variables to store data and track progress.

  • Core Logic: Assignment: The fundamental structure for storing data in a variable. The expression on the right is always evaluated before its result is stored in the variable on the left.

    
    // Assign a literal value
    
    myVariable <- 100
    
    
    // Assign the result of an expression
    
    myVariable <- anotherVariable * 2
    

Core Skill Check

  • Logic Tracing: What are the final values of x and y after this pseudocode runs: x <- 50, y <- 20, y <- x + y, x <- y?

  • Debugging: Identify the logic error in this pseudocode snippet, which is intended to calculate a final price: DISPLAY(finalPrice), finalPrice <- initialPrice + tax.

  • Application: Describe how a social media application might use a variable to keep track of the number of "likes" on a post.

Common Misconceptions & Clarifications

  • Confusing Assignment (<-) with Equality (=): The assignment operator <- is a command to do something: "put this value in this variable." It is not a statement of mathematical fact. x <- x + 1 is a valid command, whereas x = x + 1 is a mathematical impossibility.

  • Thinking a Variable is Permanently Linked to its First Value: A variable is just a container. Its contents can be changed at any time with another assignment statement.

  • Assuming Assignment is Two-Way: The statement a <- b copies the value from b into a. The variable b is not affected by this operation; its value remains the same.

  • Using a Variable Before Assigning It a Value: A variable has an undefined value until you explicitly assign it one. Trying to use it in an expression before assignment will cause an error.

Summary

Variables are a fundamental concept in programming, serving as named containers for storing and managing data. They are a form of abstraction, allowing us to work with data without worrying about the physical complexity of computer memory. The assignment operator (<-) is the mechanism used to place a value, or the result of an expression, into a variable. By updating the values of variables as a program runs, we can create dynamic, responsive, and useful software. Mastering the ability to declare, assign, and trace variables is a foundational step in learning to think computationally.