Topic 13
More on Loops
1

Computing Total in Loop
Suppose we want to find the total of numbers
1
to 4.
We use the for statement to generate the
numbers:
for ( int number = 1; number <= 4; number+
+ )
To find the total, we start with a total of 0 and
keeping adding each number to the total until we
get the total of all the numbers.
2

Computing Total in Loop
total
numbe
r
0
0 +
1
=
1
3
1
2
3
4
5 (exit
loop)

Computing Total in Loop
total
numbe
r
0
1
1
+ 2 =
3
4
1
2
3
4
5 (exit
loop)

Computing Total in Loop
total
numbe
r
0
1
3 + 3 =
6
3
5
1
2
3
4
5 (exit
loop)

Computing Total in Loop
total
numbe
r
0
1
2
3
4
5 (exit
loop)
6
6 + 4 =
10
1
3
6

Computing Total in Loop
Total
Numbe
r
7
1
2
3
4
5 (exit
loop)
0
6
10
1
3

Computing Total in Loop
// initialise total to 0 once before loop
starts
for ( int number = 1; number <= 4; number++ )
{
// add each number to total in loop body
}
// display the total once after the loop
ends
8

Computing Total in Loop
int total = 0;
for (int number = 1; number <= 4;
number++
)
{
total += number;
}
cout <<
"
Total of 1 to 4 is
"
<< total <<
endl;
total
number
number <= 4
0
1
true
(0+1 = ) 1
2
true
(1+2 = ) 3
3
true
(3+3 = ) 6
4
true
(6+4 = ) 10
5
false
9

Computing Total in Loop
Example – adding a set of
n
numbers:
int n,
total = 0;
cout << "Enter number of values to add: ";
cin >>
n
;
for ( int count = 1; count <=
n
; count++ )
{
cout << "Enter number to add: ";
cin >> number;
total += number;
}
cout <<
"
The total is
"
<< total << endl;
10

Computing Total in Loop
11

Computing Product in Loop
Example:
int n, product = 1;
// cannot initialise to 0
cout << "Enter number of values to multiply: ";
cin >> n;
for ( int count = 0; count < n; count++ )
{
cout << "Enter number to multiply: ";
cin >> number;
product *= number;
}
cout << "The product is " << product << endl;
12

Computing Product in Loop
13

14
Looping Applications
In
this
section,
we
examine
four
common
applications for loops: summation, product, smallest
and largest. Although the uses for loops are virtually
endless, these problems illustrate many common
applications.
Summation
Powers
Smallest and Largest
Topics discussed in this section:

15
Summation and Product Loops

16
FIGURE 6-23
Smallest and Largest Loops

17
To find the largest, we need to initialize the smallest variable to a very small
number, such as INT_MIN.
To find the smallest, we need to initialize the result to a very large number,
such as INT_MAX.
Note

Finding largest value in LoopExample – finding largest in a set of nnumbers:int n, largest = INT_MIN;cout << "Enter number of values to compare: ";cin >> nfor ( int count = 1; count <= n; count++ ){cout << "Enter number to compare: ";cin >> number;if(largest < number)
;
largest = number}cout << "The largest number is "<< largest << endl;18

Finding smallest value in Loop
Example – finding smallest in a set of
n
numbers:
int n,
smallest = INT_MAX;
cout << "Enter number of values to compare: ";
cin >>
n
;
for ( int count = 1; count <=
n
; count++ )
{
cout << "Enter number to compare: ";
cin >> number;
if(smallest > number)
smallest = number
}
cout <<
"
The smallest number is
"
<< smallest << endl;
19

Case Study 1
Problem:
A company has several employees whose pay
must be calculated.
The total pay for all
employees must also be calculated.
Understand the problem:
1.
