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:
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!
Here’s how we create a variable named
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
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.
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
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?
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;
scorevariable is storing the value 5 instead of the initial value of 0.
varin 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
scorevariable changes over time.
Let's step through this program, one statement at a time:
|1||Initializes the variable |
|2||Initializes the variable |
|3||Displays current value of score: |
|4||Displays current value of lives: |
|5||Re-assigns the variable |
|6||Displays current value of score: |
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
For example, you might see pseudocode like this:
age ← 21
That means the variable
ageis assigned the value
Let's look at the equivalent code in a few textual languages:
Variable assignment is also a concept in block-based languages:
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.
Check your understanding
var x = 200;
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;
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)
- 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)
- 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)