Variable
A variable is a named storage location used to hold data in a program. Its value can change during the execution of the program.
Why variables are used
Programs need to store information while they run.
Variables allow a program to keep track of data such as:
- Numbers
- Text
- User input
- Calculation results
Without variables, programs would not be able to process or reuse data.
Why learning loops matters
Loops help you:
Here:
- age is the variable name
- 20 is the value stored in the variable
The value can later be changed:
Variable name
Each variable has a name called an identifier.
A good variable name should:
Describe the data it stores
- Be easy to read
- Follow language rules
Example:
Variable assignment
Assigning a value to a variable is called assignment.
Example:
The program stores the value 100 inside the variable score.
Variables and memory
Variables represent locations in computer memory.
The programming language manages how the value is stored and accessed.
Different variables may store different data types such as numbers, text, or logical values.
Why variables are important
Variables make programs flexible because they allow data to change.
They help programs:
- Store information
- Perform calculations
- Track results
- Process user input
Almost every program relies on variables.
Simple example
Imagine a labeled box. The label is the variable name,
and the content inside the box is the value. The content can be replaced at any time.
Related terms
- What is Data Type?
- What is Function?
- What is Assignment?
Source
Information simplified from the Wikipedia article “Variable (computer science)”.