Main content
AP®︎/College Computer Science Principles
Course: AP®︎/College Computer Science Principles > Unit 3
Lesson 2: VariablesStoring data in variables
Computer programs instruct computers how to process data.
Sometimes the data comes from a user, like when a game asks you to pick a difficulty level and music volume:
Other times, the data is already stored in the program, like when that same game starts off with a score of 0 and 3 lives:
Either way, the program can store that data in variables. Each variable has a name, a value, and a type. The value might change over time, and that’s why its “variable.”
That game is using at least four variables:
name | value | type |
---|---|---|
volume | 3 | number |
difficulty | "easy" | string |
score | 0 | number |
lives | 3 | number |
Many variables store numbers and strings, like the ones above. Variables can also store other types of data, like lists, dictionaries, and Boolean values (true/false).
We'll start by learning numbers and strings, and dive into advanced types later. First things first, let's write some code!
Assigning variables
Here’s how we create a variable named
score
in JavaScript:var score = 0;
That line of code is called a statement. All programs are made up of statements, and each statement is an instruction to the computer about something we need it to do.
Let's add the
lives
variable:var score = 0;
var lives = 3;
Now our code is using two statements to store two variables with two different values. Behind the scenes, the computer is storing each value in a different place in memory and the variables point at that memory location.
Displaying variables
How do we make sure the computer is actually storing those values in memory? We can ask it to display the values.
As we learned before, one way to display on Khan Academy is with the
println()
procedure:When we instruct the computer to
println(score)
, the computer has to look in its memory for the current value of that variable, and then once it finds it, it can display it in the console.🔍 Try it yourself: Edit the code to ask the computer to find a variable that it doesn't know about yet. What happens?
Re-assigning variables
In a game, the player's score and their number of lives don't stay the same. The score typically goes up, and the lives typically go down. That means we need to be able to change the value of those variables later.
We can re-assign a variable to a new value using code similar to our initial assignment:
score = 5;
Now the
score
variable is storing the value 5 instead of the initial value of 0.Notice that when we reassign in JavaScript, we no longer put the
var
in front. That's not the case in all languages, so it may be slightly different in another language you're learning.Here's a program that creates the two variables, re-assigns one of them, and displays values along the way:
This program is interesting because it contains 2 lines that look exactly the same:
println(score)
. Yet all of the outputted lines are different; how can that be?That's because the computer runs each statement in order, and the value of the
score
variable changes over time.Let's step through this program, one statement at a time:
Step | Statement | Description |
---|---|---|
1 | var score = 0; | Initializes the variable score to 0 |
2 | var lives = 3; | Initializes the variable lives to 3 |
3 | println(score); | Displays current value of score: 0 |
4 | println(lives); | Displays current value of lives: 3 |
5 | score = 5; | Re-assigns the variable score to the value 5 |
6 | println(score); | Displays current value of score: 5 |
When we're first developing a program, we often display the value of variables to double-check the state of the program. Are the variables storing what we think they're storing, or did our code do something unexpected along the way?
🔍 Try it yourself: edit that code above and see how the output changes.
Pseudocode for variables
This pseudocode represents assigning a variable:
a ← expression
Whenever you see that pseudocode, it means that the variable
a
(or whatever it's called) is assigned the value of expression
.For example, you might see pseudocode like this:
age ← 21
That means the variable
age
is assigned the value 21
.Let's look at the equivalent code in a few textual languages:
language | code |
---|---|
JavaScript | var age = 21; |
Python | age = 21 |
Variable assignment is also a concept in block-based languages:
language | block |
---|---|
Snap! | |
AppInventor |
As you can see, the code to assign variables is very similar across languages. There are some differences though, and that's why we use pseudocode: to communicate the concept and not worry about the exact syntax.
🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!
Want to join the conversation?
- How can variables be used in a display fuction.(5 votes)
- In case you are still wondering, variables can be used by themselves like so:
var carSpeed = 50;
println(carspeed);
However, you could also join it with other things like so:
var carSpeed = 80;
println("You are going "+carSpeed+" MPH.");(1 vote)
- Since a pseudocode is same for a particular statement written in different programming languages, then why don't we have a programming language which accepts a pseudocode and converts it to directly to the programming language of our choice ?(2 votes)
- Pseudocode is more humans to understand. If we were to have a language that could understand it, it would take a lot of resources and the computer would be limited in capability. Basically, he easier it is for humans to understand,the harder it is on the computer.(6 votes)
- How can you change a variable by a number. For example, when you click a balloon, the variable "points" would change by five, so it could be 10, or any factor of 5.(2 votes)
- Suppose we have a variable named "points" that we would like to increment by 5. We can use 'points = points + 5' or 'points += 5'. Both lines of code increase the variable by 5, but the second is a shorter form that is present in Javascript (and some other programming languages as well).(4 votes)
- Is the assignment always on th left?
Does the part expression only refered to variable value or to the whole code?(2 votes)- An expression is always a term that can be evaluated to get its value. So 101 OR 110 is an expression, 1 + 1 is an expression, the variable var is an expression.
Generally, the assignment is on the left, it's technically possible to write a programming language with a different syntax (meaning assignments are on the right), but it would be really unusual.(3 votes)
- What is the use of pseudocode?(2 votes)
- Pseudocode allows us to write algorithms in a way that is easy for humans to understand without worrying about the exact implementation details or using language-specific syntax.(3 votes)
- How could you add the score variable and print the score with a click of a button? Would you have to make a whole new button?(3 votes)
- For some reason, in the assigning variables segment, the second line of code is not replacing the first input automatically. Is that supposed to happen?(2 votes)
- maybe try:
var= input; (set) true maybe you can do something with this(1 vote)
- How do you upload an image from photos in JavaScript(2 votes)
- What are dictionaries?(1 vote)
- Dictionaries are a data structure that stores Key-Value Pairs. When inserting a value in the dictionary, you also provide a key that is needed to later retrieve the value. (This is similar to how you provide an array/list an index that indicates where you are putting/getting a value.) For example, if you have a dictionary for a user account, you could have "Bob" stored with the key "Name" and "23" stored with the key "Age". Then, you could later lookup "Name" or "Age" in the dictionary to find "Bob" and "23" respectively.(2 votes)
- how would i calculate the next varible(1 vote)