Chapter 4
Chapter 4
REPETITION/ITERATION
CONTROL STRUCTURE
Prepared by: Siti Hasrinafasya Che Hassan
Reference to: D.S. Malik
Overview
2
Introduction to Repetition Structure
Repetition control structure allows a sequence of statement to
be executed repeatedly until certain condition is reached.
• Requirement of a repetition structure:
1. Loop Control Variable (LCV):
→ a variable that determines
whether the loop body will be for(<condition>)
executed.
{
2. Loop condition:
<body>
→ if the condition is true, the
loop body is executed, }
otherwise the loop exits.
3. Loop body:
→ statement to be repeated.
3
Introduction to Repetition Structure
• Execution of the loop body is controlled by 3 operations:
1. Initialization of LCV
2. Evaluation of the loop condition
3. Update LCV by incrementing or decrementing
8
For Loop
9
For Loop (Examples)
• Example 1:
Sum up the sequence of numbers starting from 1 until 10
int sum = 0; 1. Execute the initialization
1 2 3
for (int number = 1; number <= 10; number++) statement.
{ 2. While the condition remains true,
2 execute the block of statement(s).
sum += number; // sum = sum + number; 3. Then, update expression.
}
cout << “The sum is “ << sum << endl;
• Example 2:
Find the summation of 5 numbers entered by an user
int num, sum = 0;
for (int i = 1; i <= 5; i++)
{
cout << “Enter a number “ << i << “: “;
cin >> num;
sum += num;
}
cout << “The sum is “ << sum << endl; 10
For Loop (Examples)
• Example 3:
Find the maximum number among 6 positive numbers entered by
user
Int num, max = 0;
for(int count = 0; count <= 5; count++)
{
cout << “Enter a number “ << (count + 1) << “: “;
cin >> num;
if ( num > max)
max = num;
}
cout << “The maximum number is “ << max << endl;
11
While Loop
13
While Loop (Example - Sentinel)
Sentinel controlled loop
• Example 2:
Calculate the square root of any numbers as long as the number
entered is greater or equal than 10. Then, count how many numbers
that have been calculated by a program.
int x, s, count = 0;
cout << " Enter any number: ";
cin >>x; Get the sentinel value
while (x >= 10) Evaluate loop condition
{
count = count + 1;
s = sqrt(x);
cout << " Square root of " << x << " is " << s << endl;
cout << " Enter any number: ";
cin >> x; Get back the sentinel value
cout << endl;
}
cout << “The numbers that have been calculated is “ << count; 14
While Loop (Examples)
• Example 3:
Find the maximum number among 6 numbers entered by user
int x, max = 0;
int count = 0;
while (count <= 5)
{
cout << “Enter a number “ << (count + 1 ) << “: “ ;
cin >> x;
if ( x > max)
max = x;
count++;
}
cout << “The maximum number is “ << max << endl;
15
While Loop (Examples)
• Example 4:
Find an average salary entered by the user
double salary, avg;
double sum=0; if (count > 0) {
int count=0; avg = sum /count;
bool more = true; Use bool data type cout << "The average salary is " << avg << endl;
}
16
Checkpoint 1
17
Checkpoint 2
2 i) Find an average of quizzes for a student.
ii) Extends program 2i) above, repeat until an user enters
negative marks (including zero).
iii) Modify program 2ii) above, where an user is given an
option to stop the program.
iv) Change program 2iii) above, in which the program will
continually procced until -1 is entered.
Use the following outputs:
Program iv)
Program iii) 18
Do…While Loop
Initialization
Syntax:
Initialization
Action(s)
<initialization>;
do
{
Updating
Action(s) <statementBlock>;
<update>;
} while (condition);
Updating
Pretest for for and while loops Posttest for do…while loop
19
Do…While Loop (Examples)
1 int i = 0; int i = 0;
do while (i <= 20)
{ {
cout << i << “ “; cout << i << “ “;
i = i + 5; i = i + 5;
} }
while (i <= 20);
20
Nested Loop
Outer loop
{ intialization2;
Inner loop statementBlock2; while (condition 2)
} {
statementBlock1; statementBlock2;
} updating2;
}
statementBlock1;
updating1;
}
Nested for loop Nested while loop
21
Nested Loop (Examples)
• Example 1:
There are 30 students in a class. Find an average of 5 quizzes for
each student.
const int noOfStudents = 30; if(sumQuiz > 0)
const int noOfQuiz = 5; {
float quizMarks; avgQuiz = sumQuiz/noOfQuiz;
}
for(int countStud = 1; countStud <= noOfStudents; countStud++) cout << " The average of quiz marks for student "
{ << countStud << " is " << avgQuiz
float sumQuiz = 0.0, avgQuiz = 0.0; << endl << endl;
}
cout << " Student " << countStud << endl;
cout << " " << setw(20) << setfill('-') << endl;
for(int countQuiz = 1; countQuiz <= noOfQuiz; countQuiz++)
{
cout << " Enter marks of quiz " << countQuiz << ": ";
cin >> quizMarks;
22
Nested Loop (Examples)
• Example 2:
Extends from the program in Example 1, find the highest marks
of quiz obtained for each 30 students.
const int noOfStudents = 30; cout << " The highest of quiz marks for student "
const int noOfQuiz = 5; << countStud << " is " << highestQuiz
float quizMarks; << endl << endl;
24