Professional Documents
Culture Documents
91
Chapter Six: Loops
6.1 Introduction
-In programming languages, loops are START
used to execute a set of statements
repeatedly until a particular condition is Iß1
unsatisfied.
No
-This sequence of statements to be
I <= 10 END
executed is kept inside the curly braces {
} known as the Loop body. Yes
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
92
- The other form is:
-The condition is a relational expression (example: i <= 10) that determines when
the loop exits.
initialization Ex: i = 1
outside the loop
increment Ex: i = i + 1
inside the loop
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
93
-We must separate these three major sections by semicolons. The for loop
continues to execute as long as the condition is true. Once the condition becomes
false, program execution resumes on the statement following the for. Ex: a for
loop for printing 1 2 3 4 5 6 7 8 9 10
int i;
for (i = 1; i <= 10; i = i + 1)
cout << i << " ";
START
initialization: i = 1 (executed only once)
iß1
condition: i <= 10
repeated statement: cout << i << " ";
No
i <= 10 END
increment: i = i + 1
output: 1 2 3 4 5 6 7 8 9 10 Yes
PRINT i , “ “
ißi+1
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
94
-We can rewrite the previous example START
as:
for (int i = 1; i <= 10; ++i)
iß2
cout << i << " ";
from 31 to -17
k ß 31
for (int k = 31; k >= -17; k = k - 2)
cout << k << " ";
No
Output: k >= -17 END
31 29 27 25 23 21 19 17 15 13 11 9 7 5 3
1 -1 -3 -5 -7 -9 -11 -13 -15 -17 Yes
or PRINT k , “ “
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
95
6.3 Examples
Example 1
Write a complete program to print the numbers from X to Y where X, Y are
integer numbers and Y > X.
JßJ+1
Questions:
1- Print the odd numbers from -20 to 20 in different ways.
2- Print the letters from ‘A’ to ‘Z’ using a loop.
3- Print the following sequence, in addition, calculate their summation:
-7 -4 -1 2 5 8 11 14 17 20 23
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
96
Example 2
START
-Write a complete program that prints the
maximum number among n numbers. Read n, num
END
-Now, suppose n is 6 (that is you will enter six numbers). First num you enter it is
outside the loop and is assigned to max, therefore, we have max ß num.
suppose num is 555, then
num max
555 555
- The loop starts with 2 since first number 555 has been read and it has the sequence 1.
Entering the loop with
num max j
555 555 2
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
97
No
j (j<= 6) num (num > max) max J <= n
4 true 100 false 600 Yes
Read num
j (j<= 6) num (num > max) max
5 true 701 true 701 num > max
No
Yes
j (j<= 6) num (num > max) max
max ß num
6 true -90 false 701
j (j<= 6)
7 false
jßj+1
END
Questions:
1- Solve the same previous example but for finding the minimum number.
2- Solve the same previous example but for finding the maximum and the
minimum numbers.
3- Calculate and print the areas of n circles.
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
98
6.4 Nested for Loops
-The for loop can be nested. The statement that is going to be repeated is another
for loop statement.
for (initialization; condition; increment/decrement)
for (initialization; condition; increment/decrement)
statement;
return 0; Yes
}
PRINT “* “
col ß col + 1
row ß row + 1
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
99
Example 4
#include <iostream>
using namespace std;
int main() {
int row, col;
for (row = 1; row <= 5; row++) {
for (col = 1; col <= row; col++)
cout << "* "; // repeated row * col times
cout << '\n'; // repeated row times
} // row
return 0;
}
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
100
Example 5
#include <iostream>
using namespace std;
int main() {
int i, j, k;
cout << 'i' << '\t' << 'j' << '\t' << 'k' << '\n';
for (i = 1; i <= 4; i++)
for (j = 1; j <= 3; j++)
for (k = 1; k <= 2; k++)
cout << i << '\t' << j << '\t' << k << '\n';
return 0;
}
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
101
6.5 The while Loop
-The while loop is similar to the for loop but here the programmer has the
responsibility to write the initialization, condition, and the increment/decrement
in the right places.
increment ißi+1
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
102
Example 6: Write part of a program to calculate and print the summation and
average of n numbers (use while).
int n, i;
double num, sum, ave;
cout << "Enter n: ";
cin >> n;
i = 1;
sum = 0;
while (i <= n){
cout << "\nNew Number: ";
cin >> num;
sum += num; // sum = sum + num
i = i + 1;
} // while
ave = sum / double (n);
cout << "\n\nsum = " << sum << "\nave = " << ave;
Example 7: Write part of a program that reads a positive integer number. The program
prints all the numbers that divide the original number with no remainder (use while).
int i, n;
cout << " Enter an integer number n: ";
cin >> n;
i = 1;
while (i <= (n / 2)) {
if (n % i == 0)
cout << i << " ";
i++; // i = i + 1
}
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
103
6.6 Nested while
The while loop can also be nested, that is, the statement that is effected by the
while loop is another while loop.
Example 8: Write a program to calculate and print the multiplication table (1×1 to
3× 3). (use while).
int i,j;
i = 1;
while (i <= 3){
j = 1;
while (j <= 3) {
cout << i * j << '\t';
j += 1;
} // while j
i += 1;
cout << '\n';
}//while i
Question:
Resolve the program using only two for
statements, then one for statement and
one while statement.
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
104
START
initialization
Repeated
Statement(s)
Yes
condition
No
END
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
105
Example 9
Write a program to print the following series: 7 9 11 13 15 17 ………101
besides to calculate and print the summation.
START
#include <iostream>
using namespace std; i ß 7
int main() { Sum ß 0
int i, sum = 0;
i = 7; PRINT i
do {
cout << i << `\t`; sum ß sum + i
sum = sum + i;
i += 2; // i=i+2 i ß i + 2
}
while (i <= 101); Yes
cout << "\n Sum = " << sum<< '\n'; i <= 101
return 0;
} No
PRINT sum
END
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
106
Example 10
Write a program to calculate the minimum number among five numbers you read
them (use do while).
START
return 0;
No
}
PRINT min
END
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
107
#include <iostream>
using namespace std;
int main() {
int k;
double num, min;
k = 1;
cout << "First number: ";
cin >> num;
min = num;
do {
cout << "Next number: ";
cin >> num;
k = k + 1; // k++
if (num < min)
Note that the condition
min = num;
(k != 5)
} while (k != 5);
can be rewritten as
cout << "\n Minimum = " << min <<‘\n’;
(k < 5) or it can be (k <= 4)
return 0;
}
Example 11
Nested do while: Write a program to calculate and print the multiplication table
(1×1 to 5×6). (use do while)
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
108
START
#include <iostream>
using namespace std; iß1
int main() {
int i, j; jß1
i = 1; //row counter
do { PRINT i * j
j = 1; //col counter
jßj+1
do {
cout << i * j << '\t';
Yes
j++; // incr th col j <= 6
} while (j <= 6);
No
cout << '\n';
i++; // incr the row ißi+1
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023
Chapter Six: Loops
109
6.8 A Comparison between the Loops
-Example: Print the numbers from 1 to 5
Introduction to Algorithms Lecture Notes Version 5. Prepared by Dr. Alaa Al-Obaidi. January 2023