A variable is a placeholder for a value of a specified type that may change during the lifetime of the variable. A constant is a placeholder which value does not change. Variables may be initialized a a part of the declaration of the variable; a constant must be initialized when declared.
variable_type variable_definition, variable_definition, ... |
A variable definition has one of two forms. The first form initializes the variable with the result of evaluating the expression. The second form introduces an uninitialized variable.
var_name = expr or var_name |
The variable name has one of two forms. The first form defines a simple variable, while the second form defines an array.
var_id or var_id dimensions |
The dimensions give a range for each dimension of the array. The form of the array dimensions is the same as for type declarations.
Some examples of variable declarations are the following:
int i, j, k # three integer variables
int front = 0, rear = 0 # two initialized integers
string[80] line # string of up to max 80 chars
person k = person("karl", 92) # initialized record
int a[10] # an array of 10 integers
int char_count[char(0):char(127)]
# array of 128 ints, 7-bit integer indices.
bool boolean_matrix[N,N] # a 2D matrix of booleans
|
Note that the number of elements of boolean_matrix references N, which must be declared previously and given a value.
[10] int a [char(0):char(127)] int char_count [N,N] bool boolean_matrix |
Since a matrix may be seen as an array of arrays the boolean matrix may equivalently be defined as
[N] bool boolean_matrix[N] |
More variations are possible. We refer the interested to the grammar for MPD.
| lb | returns the lower bound of its argument | |
| ub | returns the upper bound of its argument |
int a[10]; real q['a':'z', -2:4] |
The following are some values of lb and ub for these arrays:
| expression | value | expression | value | |
| lb(a) | 1 | lb(q,1) | 'a' | |
| ub(a) | 10 | lb(q,2) | -2 | |
| lb(q) | 'a' | ub(q,1) | 4 | |
| ub(q) | 'z' | ub(a,1) | 10 |
A constant is declared in the same manner as variables but preceded by the keyword const. Constants must be initialized at the point of their definition.
const const_type const_definition, const_definition, ... |
The constant definition has only one form:
const_id = expr |