A girl and a robot thinking together

Think together · Start with a story

A girl keeps score for the robot. It earns 3 points, then 2 more. If the paper has just one box for the “current score,” how should she update it?

Break down the process

Follow the numbered steps, then explain them in your own words
  1. Createscore stores 3
  2. ReadGet the current 3
  3. Calculate3 plus 2 gives 5
  4. Updatescore stores 5

A variable lets a program store and access data using a name. The box analogy helps distinguish the name from the current value.

score can keep the same name while its value changes from 3 to 5.

01What is a variable, really?

Programs often need to remember a number, a character, or a true-or-false state for a while.

Name

For example, score, so the program can refer to it.

Type

For example, int, which tells the program what kind of data it can hold.

Value

For example, 95, the data currently stored.

02How do you create a variable?

In C++, a simple variable declaration looks like this:

int score;

int is the type, and score is the variable's name.

To give it a value right away, write:

int score = 95;

03Why do variables need names?

Names help programmers clearly express what the data represents.

Unclear names

a, x1 — their purpose is hard to see.

Clearer names

score, age, studentCount

A variable name is not there to explain the meaning to the CPU. It mainly helps people write and maintain programs; after compilation, the name may not remain as-is in machine code.

04Why can a variable’s value change?

A variable is called “variable” because, when the rules allow it, the value it stores can change.

int score = 10;
score = 20;
score = score + 5;

After these statements run, score has the value 25.

05Does all data have to go in a variable?

No. Programs also use constants, literals, expression results, objects, array elements, and other forms of data.

A variable is just one of the most common tools for storing and working with data.

std::cout << 100;

Here, 100 is an integer literal. It was not first stored in a variable.

06How long does a variable exist?

A variable does not necessarily “exist from the start of the program until it ends.” Different variables have different lifetimes.

For example, a local variable inside a function usually begins to exist when execution enters its scope and reaches the end of its lifetime when execution leaves that scope.

What you have learned

  • A variable is a named data object in a program that can hold a value.
  • A variable usually has a name, a type, and a value.
  • The “little box” analogy can help at first, but a variable is not a real box.
  • In C++, = usually means assignment, not mathematical equality.
  • Variables have lifetimes; they do not exist forever.

Next: Where Do Variables Live in Memory?

Your turn to try

If you change score to 5, will reading score give you both 3 and 5?

Ready? Open to see the explanation

No. This ordinary integer variable currently stores 5. To keep the history of scores, save the old value separately.