You are on page 1of 10

Sequential Statements and Variables

In the previous lecture, we wrote a simple program to display two lines of output. These two lines were
executed in sequence, the first followed by the second. In this lecture, the sequential execution of
program statements in a computer program will be discussed. We will then show how a program can
store and manipulate data in memory using the concept of a variable.

2.1 Sequential Execution of Statements

Consider again the HelloWorld program from the previous lecture:

#include <iostream>

using namespace std;

int main () {
cout << "Hello World of Computer Programming!";
cout << endl;
cout << "Here I come!";

return 0;
}

When compiled and executed, the program produces the following output:

Hello World of Computer Programming!


Here I come!

It should be observed that the cout statements are executed in the order they are written in the source
program, starting from the top. Suppose the cout statements are written in reverse order like this:

#include <iostream>

using namespace std;

int main () {
cout << "Here I come!";
cout << endl;
cout << "Hello World of Computer Programming!";

return 0;
}

When compiled and executed, the program generates the following output:

Here I come!
Hello World of Computer Programming!

1
This is because the cout statements are executed in the order (or sequence) in which they are written.
In general, the statements in a computer program are executed sequentially, starting from the top. An
important aspect of computer programming is determining the right sequence of instructions to use in
order to produce the desired results.

2.2 Programming Template

Close observation of the programs written so far reveals that each program has a basic structure as
follows:

#include <iostream>

using namespace std;

int main () {

Program Statements

return 0;
}

The highlighted text is common to all the programs that will be written in this course. A template
containing only the highlighted text above, Template.cpp, is available for download at the course Web
site. It can be used as the starting point for any program you have to write. The program statements
must be written in the position shown and will be different for each program. The highlighted text will
be discussed in the second half of in the course.

2.3 Storing and Manipulating Data

The HelloWorld program is simple and does not store or manipulate data. A typical computer program
stores and manipulates many kinds of data. We will initially look at integer data only, i.e., data in which
the values are whole numbers, e.g., 1, 10, 100, etc.

Data are stored in memory locations on a computer. The amount of memory locations will depend on
the problem that is being solved by the computer program. For example, suppose we want to write a
computer program to calculate the amount of carpet required to cover a rectangular room of width 10

2
metres and length 5 metres. Since memory locations can store only one value, we need a memory
location to store the width of the room and another memory location to store the length of the room.
We also need a memory location to store the area of carpet required. Figure 2.1 gives a snapshot of
memory containing the three memory locations:

Figure 2.1: Storage Locations in Memory

Because memory locations store many different kinds of data, it is important to give each memory
location a name so that the program can specify exactly which memory location it is referring to. Thus,
we can give the memory location with the value 10 the name “width”, the one with the value 5 the
name “length”, and the third one to store the area, the name “area”. When we give a name to refer to a
memory location, the memory location is called a variable. Thus, width, length, and area are variables.
Figure 2.2 is a picture of memory with the named memory locations or variables:

Figure 2.2: Named Storage Locations (Variables) in Memory

3
A variable name should be carefully chosen to give an indication of the data contained in the memory
location. It would be inappropriate to name the memory location for storing the width of the room
“john” or “value1”.

A variable name can consist of:


(a) Letters of the alphabet
(b) Digits
(c) Less than or equal to 1024 characters, the first of which must be a letter; however, it is
generally easier to work with short meaningful names in a program.
(d) Combinations of letters, digits and the underscore. No punctuation characters, spaces or
special symbols (such as the dollar sign and percent sign) are allowed.
(e) The name cannot be a keyword (a word that has a special meaning in a programming
language); such as the word int- because a keyword has a special meaning in C++.

2.4 Declaring Variables

If a computer program wishes to reserve a memory location to store an integer value and give it a name,
the variable name must be declared. Declaring an integer variable tells the computer to reserve a
memory location to store an integer value and give it the name specified.

The keyword int is used to declare an integer variable. In order to declare that width, length, and area
are integer variables, the following statements should be written before the each variable is used:

int width;
int length;
int area;

The individual declarations can go on one line as follows:

int width, length, area;

If a variable is used in a program but has not been declared, the program will not compile and the
compiler will indicate that the variable was not declared. This error is easy to correct by declaring the
variable.

4
2.5 Assignment Statements

Once a variable is declared, we need to know how to put (or write) a value in the corresponding memory
location and how to get the value out (or read the value from the memory location). This is done by
using assignment statements. There are two parts of an assignment statement, the value you want to
assign and the name of the memory location where the value will be stored.

For example, to store the value 10 in the variable width, the following statement is used:

width = 10;

Similarly, to store the value 5 in the variable length, the following statement is used:

length = 5;

In each assignment statement above, there is a value on the right hand side, and a variable name on the
left-hand side of the equals (“=”) symbol. The computer takes the value on the right hand side and
copies this value to the named memory location, on the left.

After the two statements are executed, the picture of memory is shown in Figure 2.3.

Figure 2.3: Picture of Memory After Executing Assignment Statements

As Figure 2.3 shows, the area memory location still does not have a value. The value for the area
variable can be calculated by multiplying the width of the room by the length of the room. The value of

5
the width of the room is currently stored in the variable width; similarly, the value for the length of the
room is currently stored in the variable length. The following statement multiplies the value stored in
the width variable (10) by the value stored in the length variable (5) and stores the result (50) in the area
variable:

area = width * length;

Note that the “*” is used for multiplication in most programming languages.

After this statement is executed, the picture of memory is shown in Figure 2.4.

Figure 2.4: Picture of Memory After Area is Calculated

2.6 Output Statements with Variables

After calculating the area of carpet required, the program can terminate since it has successfully solved
the problem. However, this is not very useful since the values stored in the memory locations width,
length, and area, are not visible outside the CPU. It is common therefore to display the values of
variables so that they can be viewed by humans (as well as other computers).

To display the value of a variable on the monitor, the cout statement can be used with the name of the
variable. For example,

cout << width;

6
However, this is not too meaningful. A label can be displayed before the name of the variable to indicate
what kind of data is being displayed. For example,

cout << "Width of the room: " << width;

Note that each item of data being displayed is separated by “<<”.

The output statements to display the data stored in each memory location are shown below:

cout << "Width of the room: " << width;


cout << endl;
cout << "Length of the room: " << length;
cout << endl;
cout << "Amount of carpet required: " << area;

The cout statement which sends the output to a new line, cout << endl, can actually be combined
with the cout statement which displays the label and the data as follows:

cout << "Width of the room: " << width << endl;
cout << "Length of the room: " << length << endl;
cout << "Amount of carpet required: " << area << endl;

The complete program to determine the amount of carpet required is given below:

#include <iostream>

using namespace std;

int main () {

int width;
int length;
int area;

width = 10;
length = 5;

area = width * length;

cout << "Width of the room: " << width << endl;
cout << "Length of the room: " << length << endl;
cout << "Amount of carpet required: " << area << endl;

return 0;
}

7
2.7 Input Statements with Variables

The program to calculate the amount of carpet required executes successfully but gives the same results
every time it is executed. To find the amount of carpet required for a room with different dimensions,
the source code of the program must be modified and the program must be re-compiled and executed.
This is not very practical.

An alternative is to let the user specify the dimensions of the room. In other words, the program should
enable the user to specify the value for the width variable as well as the value for the length variable.
This can be done using the keyboard. Accepting a value from the keyboard (typed by the user) and
storing it in a variable is known as inputting a value. The cin statement is used to input a value from the
keyboard to a named memory location. For example,

cin >> width;


cin >> length;

Note that the double arrow points in the opposite direction as cout (“>>”). It is helpful to inform the
user what value has to be typed at the keyboard by using cout statements. For example,

cout << "Please enter the width of the room: ";


cin >> width;

cout << "Please enter the length of the room: ";


cin >> length;

The complete program to determine the amount of carpet required, where the dimensions of the room
is specified by the user at the keyboard, is given below.

8
#include <iostream>

using namespace std;

int main () {

int width;
int length;
int area;

cout << "Please enter the width of the room: ";


cin >> width;

cout << "Please enter the length of the room: ";


cin >> length;

area = width * length;

cout << "Width of the room: " << width;


cout << endl;
cout << "Length of the room: " << length;
cout << endl;
cout << "Amount of carpet required: " << area;

return 0;
}

Each time the program is executed, the user can specify different values for the width and length of the
room and the program will calculate and display the area correctly based on these values. For example,
suppose the values 12 and 14 are entered when the program requests the width and length of the
room:

Please enter the width of the room: 12


Please enter the length of the room: 14

The program goes on to calculate and display the amount of carpet required for the room with the given
dimension:

Width of the room: 12


Length of the room: 14
Amount of carpet required: 168

Thus, the program can successfully calculate the amount of carpet required for a rectangular room of
any dimension.

9
2.8 Exercises

1. Write a program that calculates and displays the area and perimeter of a rectangle. Your program
should allow the user to enter the length and width of the rectangle at the keyboard.

2. Write a program to calculate the energy needed to heat a body of water from an initial temperature
to a final temperature. Your program should prompt the user to enter the amount of water in
kilograms, and the initial and final temperature of the water at the keyboard. The formula to
compute the energy is:

Q = M * (finalTemperature – initialTemperature) * 4184

where M is the amount of water in kilograms, the temperatures are in degrees Celsius, and the
energy, Q, is expressed in joules.

3. The body mass index (BMI) of an individual is a value derived from the mass (weight) and height of
an individual. It is calculated by taking the weight of a person in kilograms and dividing by the square
of his/her height in metres.

Write a program which prompts the user to enter the weight and height of a person at the keyboard
and then calculates and displays the approximate BMI.

4. Write a program that displays a multiplication table for numbers, going from 1 to 12. Your program
should allow the user to enter which multiplication table to print. For example, if the multiplication
table is 6, your program should display the following:

1 x 6 = 6
2 x 6 = 12
3 x 6 = 18
4 x 6 = 24
5 x 6 = 30
6 x 6 = 36
7 x 6 = 42
8 x 6 = 48
9 x 6 = 54
10 x 6 = 60
11 x 6 = 66
12 x 6 = 72

10

You might also like