4. Variables
Previously, you have learned to write and execute very simple programs that boil down to evaluating arithmetic expressions and sending the results to standard output. In this chapter, an important aspect of common programming languages will be introduced: the facility of giving names to things. This will enable us to write some new interesting programs.
Modern programming languages have an ability
to introduce variables which are quite similar to mathematical variables. To
introduce a variable named x
we can use the following statement:
The word double
stands for "double precision floating point number". It specifies the type of variable. We will be talking about types later.
double x = 5;
This statement introduces a new name x
. More
precisely, we say that x
is a variable containing, or having as its value, the number 5. Or,
we could also say that the variable x
has been assigned the number 5. We can
also say that x
is a variable whose value is the number 5, or that its value equals
5. We can even say the variable x
assumed the value 5.
This variable x
can be
used as a part of an expression. For example, this program
#include <iostream> using namespace std; int main() { double x = 5; cout << "x plus 3 equals " << x+3 << endl; }
will result in the following output:
x plus 3 equals 8
Here is another variable:
double y= 7*3;
In all likelihood, you already know that the
little star - also known as the asterisk - is the multiplication operator. In
this statement, 7*3
is an expression, thus it evaluates to the number 21. A new
variable called y
is introduced and assigned the computed value of 21. An
important thing to understand is that variables do not contain expressions, only values.
Let us introduce one more variable in
addition to variables x
and y
:
double z = (y-x)/8;
What is the value of variable z
? If x
equals 5
and y
equals 21, then z=(21-5)/8
, and that evaluates to the number 2. In total, the variable z
should
now contain as its value the number 2.
Finally, let us unify all of this into a single program:
#include <iostream> using namespace std; int main() { double x = 5; double y = 7*3; double z = (y-x)/8; cout << "x: " << x << endl; cout << "y: " << y << endl; cout << "z: " << z << endl; cout << "x+y+z: " << x+y+z << endl; }
The program should produce the following output:
x: 5 y: 21 z: 2 x+y+z: 28
As demonstrated, variables can form expressions. More precisely, a variable name itself is a kind of a primary expression[*].
Primary expressions were explained in the chapter 'First Programs'.
Type in and execute this program using your IDE. Detailed instructions for starting the execution were given in the previous chapter. Here is a quick reminder: in Visual Studio, select Debug -> Start Without Debugging, or simply press the Ctrl-F5 key combo. In Code::Blocks, select Build -> Build and Run, or press the F9 key. |
When typing in a program you should pay extra attention to semi-colons. Should you forget one, the compiler will report an error. However, it is possible the compiler will not recognize where exactly you have gone astray and report a strange error in the next line of your program. In fact, the actual mistake is often in the line above the one where the compiler reports an error. So when searching for and correcting errors, you should always examine the line above the one indicated as well. |