Вы находитесь на странице: 1из 11

Flow Charts and Repetition Statements

This lecture presents a graphical way to represent the flow of logic in a computer program using a tool
known as a flow chart. Several examples of flow charts will be given. We will also discuss the importance
of being able to repeat a set of statements in a program. This will be shown diagrammatically using a
flow chart. A commonly used repetition statement, known as the for loop will then be presented.

4.1 Flow Charts

A flow chart is a diagram which shows the flow of logic through a computer program. In this course, we
will use a small set of symbols to draw a flow chart. These are shown in Table 4.1.

Symbol Name Description

Start / End An oval is used to indicate the start or


end point of a program.

Arrow A line with an arrow is used to show the


flow of logic from one part of the
program to another.

Input / Output A parallelogram is used to indicate input


or output of the program (e.g., cin or
cout statements).

Process A rectangle represents a processing step


(e.g., assigning the value of an expression
to a variable).

Decision A diamond indicates a decision being


made.

Table 4.1: Flow Chart Symbols

1
4.2 Sequence: Flow Chart for Calculating Cost of Carpet

In the previous lectures, we looked at a program which inputs the dimensions of a rectangular room and
determines the amount of material required to carpet the room. The flow of logic through this program
can be illustrated in the flow chart shown in Figure 4.1.

Start

input
width, length

area =
width x length

output
width, length,
area

End

Figure 4.1: Flow Chart for Carpeting Program

The logic of the program is contained between the “Start” and “End” symbols. The program first obtains
the values of the width and length of the room, which is input by the user via the keyboard. An
“Input/Output” symbol is used to depict this step. (Note that two or more input/output statements can
be combined in one “Input/Output” symbol to simplify the flow chart.) The area of the carpet is then
calculated; this is shown in the flow chart using the “Process” symbol. Finally, the width, length, and
area values are displayed on the monitor using an “Input/Output” symbol. As can be observed from
Figure 4.1, all the symbols are connected by arrows, showing the sequence of programming steps
required to solve the given problem.

2
Suppose we wish to include an additional feature in the program—we would like to calculate the cost of
the carpet required, given the price of the carpet. Three changes will have to be made to the program
and the corresponding flow chart:

(1) An input step will be required to obtain the price of the carpet from the user. Assume this is stored
in a floating point variable, priceCarpet. The value for this variable will be input using a cin
statement:

cin >> priceCarpet;

A new “Input/Output” symbol for inputting the value of the price of the carpet from the user has to
be added to the flow chart.

(2) A new processing step will be needed to calculate the cost of the carpet, based on the amount of
carpet required and the price of the carpet. The cost can be calculated as follows:

cost = area * priceCarpet;

A new process symbol must be added to the flow chart to depict the calculation, after the area has
been calculated.

(3) An output step will be required to display the cost of the carpet. An “Input/Output” symbol to depict
the output of the cost of the carpet must be added to the flow chart.

These three steps must be included at the appropriate place in the flow chart shown in Figure 4.1. The
revised flow chart is shown in Figure 4.2, with the new steps shaded.

3
Start

input
width, length

input
priceCarpet

area =
width x length

cost = area x
priceCarpet

output
width, length,
area

output
cost

End

Figure 4.2: Flow Chart for Enhanced Carpeting Program

4
The revised program is given below:

#include <iostream>

using namespace std;

int main ()
{
int width;
int length;
int area;
float priceCarpet;
float cost;

cout << "Please enter the width and length of the room: ";
cin >> width >> length;

cout << "Please enter the price of the carpet (per square metre): ";
cin >> priceCarpet;

area = width * length;


cost = area * priceCarpet;

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

return 0;
}

The flow chart for calculating the cost of the carpet for a certain room consists of several symbols
connected via lines with arrows. The program is strictly sequential from the “Start” symbol to the “End”
symbol. This means that the programming steps are executed one after the other, starting from the
“Start” symbol. There is no repetition of programming steps; so, once a step is executed, it will never be
executed again. In the next section, we will show how to repeat one or more steps in a program.

4.3 Repetition: Flow Chart for Displaying Multiple Lines of Output

Suppose we have to write a program to produce the following output:

**********
**********
**********
**********
**********

Five lines of output must be produced, where each line contains 10 asterisks (stars).

5
The following program can be written to produce the required output:

#include <iostream>

using namespace std;

int main ()
{

cout << "**********" << endl;


cout << "**********" << endl;
cout << "**********" << endl;
cout << "**********" << endl;
cout << "**********" << endl;

return 0;
}

Suppose that we require 10 lines of output instead of 5. How can we modify the program to generate
the required output? A simple solution is to insert five more cout statements, similar to the existing
five. Now, suppose that we require 100 lines of output instead of 10. What do we do? We can go on
inserting cout statements but after a while, you will realize that this is not a very effective approach for
producing the required output.

What we need is a way to tell the computer to execute the following statement 5 times, or 10 times, or
100 times, or indeed, any amount of times:

cout << "**********" << endl;

This can be done in most programming languages, but we must help the computer a little to keep track
of the amount of lines that have already been displayed. To do so, we will use an integer variable, count.
At any moment in time, the value in count indicates how many lines have already been displayed. At the
start of the program, no lines have been displayed as yet, so count is given the value zero:

count = 0;

Each time a line is displayed, we add one to the value currently stored in count. In the previous lecture,
we showed how to add one to the value stored in a variable count, using the following statement:

count = count + 1;

6
Suppose we have to display 10 lines of output. Before displaying a line of output, we check to see if
count is less than 10. If it is, another line of output is displayed and we add one to the value of count;
then we return to the statement which checks to see if count is less than 10. If count is equal to 10, we
end the program. Figure 4.3 is a flow chart which shows the logic of the program:

Start

count = 0

Is No
count < 10?

Yes

output
10 stars
on a line

add 1 to count

End

Figure 4.3: Flow Chart for Displaying Multiple Lines of Output

Figure 4.3 contains a “Decision” symbol. The decision that has to be made is to determine if count is less
than 10; it is represented textually as follows, “Is count less than 10?” There are only two outcomes of
this decision: Yes or No (or true or false). If the result is Yes, we would like to display another line of
output. So, a line labelled “Yes” from the “Decision” symbol goes to the “Input/Output” symbol which
displays a line of output. If the result is No, we would like to terminate the program. So, a line labelled
“No” from the “Decision” symbol goes to the “End” symbol.

7
As long as count is less than 10, a line of output will be produced and count will be updated by one.
Thus, the logic for producing a line of output and updating the count variable is repeated several times
(10 times, to be exact). So, instead of having to write 10 cout statements, we can set up a mechanism to
repeat two statements, 10 times. This is known as repetition.

4.4 Repetition: For Loop

The program corresponding to the flow chart of Figure 4.3 is given below. Repetition is achieved by
means of a for loop, a commonly used construct for repeating one or more statements in a program.

#include <iostream>

using namespace std;

int main ()
{

int count;

for (count = 0; count < 10; count = count + 1)


cout << "**********" << endl;

return 0;
}

The variable count is called a loop control variable since it controls how many times the for loop will be
executed. The for loop consists of three sections separated with a semi-colon within brackets. The first
section gives the loop control variable an initial value:

count = 0;

The second section is the decision that must be made before the loop is repeated. To specify “Is count <
10?”, the following expression is used:

count < 10

This expression will evaluate to either true or false. If it is true, the statement in the “body” of the for
loop will be executed. If it is false, the statement in the “body” of the for loop will not be executed.

8
The third section tells the program how to modify the loop control variable after each execution of the
for loop. In this case, all we want to do is to add one to its value, so we use the following statement:

count = count + 1

The “body” of the loop consists of a single statement which may be repeated several times, depending
on the value of the expression in the second section. The different parts of a for loop are shown in
Figure 4.4.

Figure 4.4: Parts of a for Loop

The steps in the flow chart of Figure 4.3 can be mapped to program statements as shown in Figure 4.4.
Note that the steps of the flow chart which may be repeated have been shaded.

When the program is compiled and executed, it generates the following output (10 lines of asterisks):

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

9
Figure 4.5: Mapping of Flow Chart Steps to Program Statements

Suppose we would like the program to display fifteen lines of output, each with 10 asterisks. A simple
approach is to change the 10 in the for loop to 15:

for (count = 0; count < 15; count = count + 1)


cout << "**********" << endl;

This is not very effective since we need to modify the for loop every time we have to display a
different number of lines. In the next lecture, we will show how to display any amount of lines without
modifying the for loop.

10
4.5 Exercises

1. What is the output produced by the following for loop?

int count;
for (count = 5; count < 10; count = count + 1)
cout << "**********" << endl;

2. What is the output produced by the following for loop?

int count;
for (count = 5; count < 15; count = count + 1)
cout << "**********" << endl;

3. What is the output produced by the following for loop?

int count;
for (count = 0; count < 10; count = count + 1)
cout << count << endl;

4. What is the output produced by the following for loop?

int count;
for (count = 0; count < 10; count = count + 1)
cout << count;

5. What is the output produced by the following for loop?

int count;
for (count = 0; count < 10; count = count + 2)
cout << count << endl;

6. Write a program to display the following figure on the monitor:

----------
| |
| |
| |
| |
| |
----------

11

Вам также может понравиться