Constant and Variable
Store data used in program:
- read in from user
- constants used in program
A data item can be declared either as a constant or a variable
- Constants are initialized with a value, but their value cannot be changed after that.
- The value of a variable can be changed as needed.
The keyword const in the declaration indicates that the data item is a constant
// Declaring constants
const int MIN_VALUE = 0;
const int MAX_VALUE; // Error
MIN_VALUE = 45; // Error
cout << “MIN_VALUE is now “ << MIN_VALUE;
Declaration of data items.
We need to declare data items in our program prior to using them.
The declaration tells:
- whether the data item is a constant or a variable.
- the identifier that will be used in the program to name the data item.
- the data type for the data item.
// Declaring a constant.
const float PI = 3.1416;
// Single variable declared at a time.
// Can declare many data-items of the same type together.
int height, base;