0% found this document useful (0 votes)
26 views5 pages

Introduction To Repetition Using For Loops

This chapter introduces the concept of repetition in programming using for loops, which allows a set of statements to be executed multiple times until a condition is met. It explains how to implement a for loop to control the number of iterations and demonstrates its use with examples of displaying asterisks and sequences of numbers. Additionally, it shows how to utilize variables within the for loop to modify the number of iterations dynamically.

Uploaded by

Adalia Mahabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Introduction To Repetition Using For Loops

This chapter introduces the concept of repetition in programming using for loops, which allows a set of statements to be executed multiple times until a condition is met. It explains how to implement a for loop to control the number of iterations and demonstrates its use with examples of displaying asterisks and sequences of numbers. Additionally, it shows how to utilize variables within the for loop to modify the number of iterations dynamically.

Uploaded by

Adalia Mahabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introduction to Repetition Using for Loops

In this chapter, we will discuss the importance of being able to repeat a set of statements in a
program. This allows one or more statements to be repeatedly executed until some condition is
met. We will show how repetition is drawn in a flowchart and then explain how it is
implemented in a program using a for loop.
The Need for Repetition
Suppose we have to write a program to produce five lines of output, where each line contains 10

asterisks (stars):

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 number of times:
This can be done by means of a repetition statement. In particular, a for loop can be used to tell
the computer to execute a set of instructions a certain number of times until some condition is
met.

To use a for loop to display the cout statement above, we must help the computer to keep track of
the number of lines that have already been displayed. This can be done using an integer variable,
count. At any moment in time, the value in count indicates how many lines have already been
displayed. At the beginning of the output, count is given the value zero since no lines have been
displayed as yet: count = 0;
Each time a line is displayed, we add one to the value currently stored in count. In Chapter 3, we
showed how to add one to the value stored in a variable count: count = count + 1;
Suppose we need 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 5.1 is a flowchart which shows the logic of the
repetition statement:

So, instead of having to write 10 cout statements, we can write the cout statement once and
repeat it 10 times. This is referred to as repetition.
Repetition with for Loops
The program corresponding to the flowchart of Figure 5.1 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.
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 less than 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 will not be executed and the loop
terminates.
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
5.2.

When the program is compiled and executed, it generates the following output (10 lines of asterisks):
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
Writing a for Loop with Variables
Suppose we would like the for loop from the previous section to display fifteen lines of output,
each with 10 asterisks. A simple approach is to change the 10 in the second section of the for

loop to 15:

We will now show how to display any amount of lines without modifying the for loop.
The first and third sections of a for loop are assignment statements. An arithmetic expression
containing variables can be written on the right-hand side of an assignment statement. So, if start
is an integer variable containing a value, the first section of the loop could be written as:
count = start;
The second section of a for loop is a relational expression (to be discussed in Chapter 10). This,
too, can be formed using one or more variables. For example, suppose numLines is an integer
variable containing the value, 10. The expression,
count < numLines
returns true as long as the value of count is less than the value in numLines, i.e., 10. If the value
of numLines is 15, the expression returns true as long as the value of count is less than the value
in numLines. Thus, by using a variable in the expression, there is no need to change the second
section of the for loop to display a different amount of lines. All that has to change is the value of
the variable (e.g., by using an assignment statement or an input statement, before the for loop is
executed). When a variable is used in the second section of the for loop, the original for loop can
be written as follows:

Using a for Loop to Display a Sequence of Numbers


Consider the following for loop:

What does this for loop display? The “body” of the for loop consists of the statement,
cout << count << endl;
This statement displays the value of the count variable on the monitor and then goes to a new
line (because of the endl keyword). The first time through the loop, the value of count is zero, so
the for loop displays a single 0 on a line by itself: 0
The value in count is updated at the end of the loop to 1 (by the statement, count = count + 1).
Since 1 is less than 10, the loop is executed again, and this time the value 1 is displayed on a line

by itself since count has the value 1. The output is now:

The value in count is updated at the end of the loop to 2. Since 2 is less than 10, the loop is

executed again and the value 2 is displayed on a line by itself. The output is now:
This continues until count is 9. The output is now:
So, the for loop generates a sequence of numbers starting from 0 and ending at 10. Let us now
consider what happens if we leave out the endl keyword from the cout statement:
cout << count;
As before, the value of count is displayed each time the for loop is executed; however, the values
are joined together in the output as follows (since there is no white space displayed between the
values of count): 123456789
To generate a different sequence of numbers, say, from 5 to 20, the first and second sections of
the for loop should be modified as follows:

It is also possible to change the interval between each number in the sequence by modifying the
arithmetic expression in the third section of the for loop. For example, if we want an interval of
5, the third section should be written as follows:
count = count + 5;
This causes the following output to be produced: 5,10,15

You might also like