Topics / Programming / Variables and Constants

Variables and Constants

During the running of a computer program, there will be times when the program needs to remember/store a value so it can be read and used later on.

In order to store a value, the program needs to establish a memorable (named) place (data structure) that it can use to hold the data.  These locations are called variables and constants.

A program can contain many variables and constants, so it is important to give them sensible names that try to describe the item of data that they hold.

The key difference between a variable and a constant is:

  • The value stored in a variable can/may change during the running of the program
  • The value stored in a constant cannot be changed during the running of the program

Both constants and variables can only hold one piece of data at a time.  When a new value is assigned to variable (using an assignment statement) it automatically replaces whatever was stored previously.

Examples:

Variable NameValue
Level4
HighScore1202
SurnameSmith
Constant NameValue
VAT20
Days365
Bonus100

 

Topics / Programming / Variables and Constants

Popular Downloads