Variables & Assignment (College Board AP® Computer Science Principles): Study Guide

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Variable fundamentals

What is a variable?

  • A variable is a named storage location in a program that holds a value

  • Variables allow programs to store, retrieve, and manipulate data during execution

  • A variable name is the label used to refer to the stored value in the code

  • Variable names should be meaningful and descriptive so the purpose of the variable is clear from its name alone; for example, studentScore is preferable to x

  • Clear, descriptive names lead to:

    • Improved readability across the codebase

    • Reduced need for additional comments

    • Easier debugging and maintenance

  • Names must follow the conventions of the programming language being used; for example, no spaces and no starting with a number

Data types & suitability

What are data types?

  • A data type defines the kind of value a variable can hold and what operations can be performed on it

  • Choosing the correct data type for a variable is important for ensuring the program processes data correctly

  • Using an unsuitable data type can cause errors or produce unexpected results

Data type

Description

Example values

Suitable for

Number

Numeric values (integers and decimals)

42, 3.14, -7

Calculations, counting, measurements

String

A sequence of characters

"Hello", "AP CSP"

Names, messages, text input

Boolean

A value that is either true or false

true, false

Conditions, yes/no decisions, flags

List

An ordered collection of values

[10, 20, 30]

Storing multiple related values in one variable

Choosing the right data type

  • The suitability of a data type depends on what the program needs to do with the value

  • A test score should be stored as a number (so calculations can be performed), not a string

  • A student's name should be stored as a string, not a number

  • A condition that is either met or not met should be stored as a Boolean

Assignment operations

How are values assigned to variables?

  • Assignment is the process of storing a value in a variable

  • In AP CSP pseudocode, the assignment operator is the left arrow:

  • The variable on the left of the arrow receives the value on the right

score ← 10
name ← "Alex"
isPassing ← true
 
  • Each assignment replaces the previous value stored in the variable (variables can only hold one value at a time)

  • The right side of the assignment can be a literal value, another variable, or an expression

How are expressions used in assignment?

  • An expression is evaluated first, then the result is stored in the variable

x ← 5
y ← x + 3
 
  • In this example, y is assigned the value 8 (the result of evaluating x + 3)

  • If x is later reassigned, the value of y does not change; it retains the value from when it was assigned

Examiner Tips and Tricks

  • The arrow operator means "is assigned the value of" — always read it from right to left. A common exam mistake is confusing assignment with comparison. x ← 5 stores the value 5 in x; it does not check whether x equals 5. When tracing code, update variable values one statement at a time and remember that reassignment overwrites the previous value.

  • For the AP Create Performance Task, use meaningful variable names that clearly describe what each variable stores — this helps both your written responses and anyone reviewing your code.

Worked Example

Consider the following code segment.

a ← 3
b ← a + 2
a ← 7
DISPLAY(b)
 

What value is displayed when this code segment is executed?

(A) 3

(B) 5

(C) 7

(D) 9

[1]

Answer:

(B) 5 [1 mark]

  • When b ← a + 2 executes a is 3 so b is assigned 5; reassigning a to 7 afterwards does not affect the value already stored in b

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.