Types, values, and expressions
This lesson explains the difference between types, values, and expressions. These concepts have already been used in previous lessons, but this lesson names them explicitly, and also explains the relations between them. Furthermore, this lesson serves as an introduction for the coming lessons, which rely heavily on these concepts. Consider the following declarations of discrete variables:
disc int x = 1;
disc int y = 2 * x;
The first declaration declares a discrete variable named x
, and the second declaration declares a discrete variable named y
. Both variables have an int
data type. A data type is usually just called a type, if there is no confusion with other kinds of types. The type of a variable indicates the potential or allowed values of the variable. Variable x
is initialized to value 1
. Variable y
is initialized to twice the value of x
, meaning it is initialized to value 2
.
Both 1
and 2 * x
are expressions. Expressions are combinations of among others literal values (e.g. 1
), variables (e.g. x
), and operations (e.g. *
) on them. Expressions can be computed, resulting in a value. This is called evaluation of the expression.
Expression 2 * x
can be evaluated. Evaluating the expression results in value 2
if the value of x
is 1
, and in value 4
if the value of x
is 2
. Expressions can thus be evaluated to different values, depending on the values of the variables that occur in them.
Expression 1
consists of only a single value, called a literal value expression. Evaluation always results in that single value. Expression 1 + 3
evaluates to value 4
. Even though it does not consist of only just a literal, the value is the same for each evaluation. The expression represents a constant value.