Variables
Concrete concepts that are represented in a program are called variables. A value like air temperature and a more complicated object like a car engine can be variables of a program.
The main purpose of a variable is to represent a value in the program. The value of a variable is the last value that has been assigned to that variable. Since every value is of a certain type, every variable is of a certain type as well. Most variables have names as well, but some variables are anonymous.
As an example of a variable, we can think of the concept of the number of students at a school. Since the number of students is a whole number, int
is a suitable type, and studentCount
would be a sufficiently descriptive name.
According to D's syntax rules, a variable is introduced by its type followed by its name. The introduction of a variable to the program is called its definition. Once a variable is defined, its name represents its value.
import std.stdio; void main() { // The definition of the variable; this definition // specifies that the type of studentCount is int: int studentCount; // The name of the variable becomes its value: writeln("There are ", studentCount, " students."); }
The output of this program is the following:
There are 0 students.
As seen from that output, the value of studentCount
is 0. This is according to the fundamental types table from the previous chapter: the initial value of int
is 0.
Note that studentCount
does not appear in the output as its name. In other words, the output of the program is not "There are studentCount students".
The values of variables are changed by the =
operator. The =
operator assigns new values to variables, and for that reason is called the assignment operator:
import std.stdio; void main() { int studentCount; writeln("There are ", studentCount, " students."); // Assigning the value 200 to the studentCount variable: studentCount = 200; writeln("There are now ", studentCount, " students."); }
There are 0 students. There are now 200 students.
When the value of a variable is known at the time of the variable's definition, the variable can be defined and assigned at the same time. This is an important guideline; it makes it impossible to use a variable before assigning its intended value:
import std.stdio; void main() { // Definition and assignment at the same time: int studentCount = 100; writeln("There are ", studentCount, " students."); }
There are 100 students.
Exercise
Define two variables to print "I have exchanged 20 Euros at the rate of 2.11". You can use the floating point type double
for the decimal value.