A variable is a named storage location in memory that holds a value. You give the variable a name, assign a value to it, and then use that name whenever you need that value.
Think of it like a labeled box: the label is the name, and whatever is inside is the value.
┌─────────┐
│ 1 │ ← value
└─────────┘
score ← nameWhen the program runs, it looks up the variable’s name and retrieves the value stored inside.
Why Variables Matter#
Without variables, a program forgets information as soon as it moves to the next line. Variables let you hold on to values — like what the player typed — so you can use them later.
Declaring a Variable#
To use a variable, you first need to declare it. Declaring a variable tells the program: “I need a storage location with this name.”
Changing a Variable’s Value#
Once declared, you can update the value stored in a variable by assigning a new one to it. The old value is replaced.
int score = 0;
score = 1;score = 0
score = 1let score = 0;
score = 1;After the second line, score holds 1.