Understanding Variables and Constants
In programming, a variable is a named storage location that can hold data which may change during the execution of a program. For example, a variable named score might store a player's current points and update as the game progresses.
A constant is similar in that it is a named storage location, but its value is fixed once set and cannot be altered during program execution. For instance, a constant named MAX_PLAYERS might hold the value 4 to represent the maximum number of players allowed in a game.
- Variables store data that can change.
- Constants store data that remains fixed throughout the program.
Declaring Variables and Constants
Before using variables or constants, they must be declared. Declaration involves specifying a name (identifier) and often a data type that defines what kind of data the storage location can hold, such as integer, real number, or string.
For example, in pseudocode: `DECLARE age AS INTEGER` creates a variable named age that can hold whole numbers. To declare a constant, the syntax might be: `DECLARE CONSTANT PI AS REAL = 3.14159` which sets PI as a fixed real number value.
Proper declaration helps the program manage memory and prevents errors by ensuring data is used consistently.
- Declaration includes naming and typing the storage location.
- Constants are declared with a fixed initial value.
- Variables can be declared without an initial value but must be assigned before use.
Using Variables and Constants in Programs
Variables are used when data needs to be stored and updated, such as counters, user inputs, or calculations. Constants are used for values that should not change, like configuration settings or fixed limits.
For example, a program calculating the area of a circle might use a constant for PI and a variable for the radius entered by the user:
`DECLARE CONSTANT PI AS REAL = 3.14159` `DECLARE radius AS REAL` `INPUT radius` `area = PI * radius * radius` `OUTPUT area`
- Use variables for data that changes.
- Use constants for fixed values to avoid accidental modification.
- Using constants improves program readability and maintainability.
Choosing Meaningful Identifiers
Names given to variables and constants should be descriptive to make the program easier to understand and maintain. For example, `totalScore` is clearer than `ts` or `x`.
Good naming conventions help prevent confusion and errors, especially in larger programs or when working in teams.
- Use clear, descriptive names for variables and constants.
- Avoid single-letter or ambiguous names unless used in very limited contexts.
- Follow consistent naming conventions throughout the program.
Common Practices and Tips
Always initialise variables before use to avoid unpredictable behaviour. Constants must be assigned a value at declaration since they cannot be changed later.
Be aware of the scope of variables and constants—where in the program they can be accessed. This affects how and where you declare them.
Use constants to represent values that might need to be changed in one place only, such as tax rates or maximum limits, making programs easier to update.
- Initialise variables before use.
- Assign constants at declaration.
- Consider scope when declaring variables and constants.
- Use constants to simplify program updates.
At-a-glance summary
Comparison table
| Aspect | Variable | Constant | Example |
|---|---|---|---|
| Mutability | Value can change during program execution | Value remains fixed after declaration | Variable: `score` changes as player earns points; Constant: `MAX_LEVEL` set to 10 |
| Declaration | Declared with a name and type; may be assigned later | Declared with a name, type, and assigned immediately | `DECLARE age AS INTEGER` vs `DECLARE CONSTANT PI AS REAL = 3.14159` |
| Purpose | Store data that varies, such as user input or counters | Store fixed values that should not change, like configuration | Variable: `temperature` changes; Constant: `FREEZING_POINT` = 0 |
| Usage in Code | Can be assigned new values multiple times | Cannot be assigned new values after initialisation | `score = score + 1` valid; `PI = 3.14` invalid if PI is constant |
Active recall
Quick check
1. What is the main difference between a variable and a constant?
A variable can have its value changed during program execution, while a constant's value remains fixed once set.
2. How do you declare a constant named MAX_SPEED with a value of 100?
Declare it by specifying it as a constant with a name, type, and assigned value, for example: `DECLARE CONSTANT MAX_SPEED AS INTEGER = 100`.
3. Why is it important to use meaningful names for variables and constants?
Meaningful names make the program easier to understand, maintain, and reduce errors by clearly indicating the purpose of each identifier.
4. Can a variable be used to store user input that changes during the program?
Yes, variables are designed to store data that can change, such as user input.
