Variables & Assignment (College Board AP® Computer Science Principles): Study Guide
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,
studentScoreis preferable toxClear, 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) |
| Calculations, counting, measurements |
String | A sequence of characters |
| Names, messages, text input |
Boolean | A value that is either true or false |
| Conditions, yes/no decisions, flags |
List | An ordered collection of values |
| 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,
yis assigned the value8(the result of evaluatingx + 3)If
xis later reassigned, the value ofydoes 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 ← 5stores the value 5 inx; it does not check whetherxequals 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 + 2executesais 3 sobis assigned 5; reassigningato 7 afterwards does not affect the value already stored inb
Unlock more, it's free!
Was this revision note helpful?