CHAPTER 4:
Repetition Control Structure
Prepared for:
CSC 402 – Programming 1
© Najwa Abd Ghafar – UiTM Johor
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
OBJECTIVES OF THIS CHAPTER
In this chapter, you will:
Learn about repetition (looping) control structures
Explore how to construct and use counter-controlled loop,
sentinel-controlled loop and flag-controlled loop
Discover how to form and use nested repetition control
structures
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
INTRODUCTION
What is the meaning of
repetition?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
CONTROL STRUCTURES
A computer program can be executed:
In sequence
By Selection (Branch): Making a choice
Control Structures
By Repetition (Iterative): Looping
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
REPETITION STRUCTURE
Repetition (Looping) Structure:
Allows a computer program to execute the same sequence of statements
over and over again
BUT this is only possible if the loop condition remains TRUE
A repetition is also called:
A loop
An iteration
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
WHY LOOPING?
Why do you need looping?
Write a program that will display the word
“Hello” 10 times.
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
WHY LOOPING?
Use repetition (looping) to repeat the same statements over and
over again
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
REPETITION STRUCTURE
Repetition (Looping) Structure:
There are 3 types of repetition structure:
1. Counter-controlled Loop
2. Sentinel-controlled Loop
3. Flag-controlled Loop
To write a program for each type of repetition structure in C++,
you could use any of these:
1. while loop
2. for loop
3. do... while loop
© Najwa Abd Ghafar – UiTM Johor
COUNTER-CONTROLLED LOOP
©Rose Hafsah Ab Rauf – UiTM Shah Alam
Is used when the number of repetition (n) is known in advance
The looping will STOP after the segment of code has been
repeated (n) times
For example:
Displaying the word “Hello” 10 times
Getting input from user 5 times
Repeating a process 3 times
The steps involved in writing counter-controlled loop:
1) Declare and initialize a CONTROL VARIABLE
(Before entering the loop)
2) Evaluate the CONTROL VARIABLE against the LOOP CONDITION
(To check whether the looping should continue or not)
3) Update the CONTROL VARIABLE at each loop
(By incrementing or decrementing)
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
while LOOP
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
a. Draw a flowchart for the program above
b. What will be the output displayed on the screen for the program above?
c. What is the purpose of the program above?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
What will be the output displayed on the screen for the program below?
What will be the output displayed on the screen for the program below?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
a. Draw a flowchart for the program above
b. What will be the output displayed on the screen if the numbers entered are
5, 2, 10, 6 and 15 respectively?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
Identify the following components in repetition control structure:
a. Loop control variable
b. Initialization statement
c. Loop continuation condition
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
Correct the following code so that it reads and finds the sum of 30 numbers
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
do.. while LOOP
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
while LOOP VS do.. while LOOP
The loop condition is evaluated The loop condition is evaluated
at the beginning of loop at the end of loop
The loop can be executed The loop must be executed
0 or more times at least once (1 or more times)
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
a. If count is initialized as 6, what would be the output displayed on the
screen?
b. Transform the program above by using do.. while loop instead of while loop
c. Using the new program, if count is initialized as 6, what would be the
output displayed on the screen?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
a. Draw a flowchart for the program above.
b. What will be the output displayed on the screen if the salary entered are
2000, 1500, 1200, 1000 and 2500 respectively?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
for LOOP
© Najwa Abd Ghafar – UiTM Johor
while LOOP VS for LOOP
©Rose Hafsah Ab Rauf – UiTM Shah Alam
Control information are
spread out in 3 different locations
Control information are all
in one place
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
Transform the program above using a for loop instead of while loop
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
a. Draw a flowchart for the program above.
b. What will be the output displayed on the screen for the program above?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
© Najwa Abd Ghafar – UiTM Johor
QUICK TIP!
©Rose Hafsah Ab Rauf – UiTM Shah Alam
© Najwa Abd Ghafar – UiTM Johor
QUICK TIP!
©Rose Hafsah Ab Rauf – UiTM Shah Alam
© Najwa Abd Ghafar – UiTM Johor
SENTINEL-CONTROLLED LOOP
©Rose Hafsah Ab Rauf – UiTM Shah Alam
Is used when the number of repetition (n) is NOT known in advance
The looping does not know when to stop because it doesn’t know
how many repetition it must do
Therefore, a SENTINEL VALUE is used to end the loop
For example:
Repeat a process as long as the user does not input “999”
(If user input 999, the loop will terminate)
Repeat a process as long as the user input “Y”
(If user input value other than Y, the loop will terminate)
The steps involved in writing sentinel controlled loop:
1) Declare and initialize a CONTROL VARIABLE
2) Evaluate the CONTROL VARIABLE against the LOOP CONDITION
3) Update the CONTROL VARIABLE at each loop
(By input received from user)
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
EXAMPLE
#include <iostream>
using namespace std;
int main(){
int bil = 0, num;
char ans='n';
while(ans!='Y')
{
bil++;
cout<<"Enter an integer number"<<endl;
cin>>num;
cout<<"To exit enter 'Y'"<<endl;
cin>>ans;
}
cout<< "Number of integers entered is "<<bil<<endl;
return 0;
}
}
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
EXAMPLE
##include <iostream>
#include <cmath>
using namespace std;
int main(){
double power;
cout<<"Enter a floating point number to compute the power of 2."<<endl;
cout<<"Enter 0 to stop the program"<<endl;
cin>>power;
while(power)
{
cout<<"The "<<power<<"th power of 2 is :"<<pow(2,power)<<endl;
cout<<"Enter a floating point number to compute the power of 2."<<endl;
cout<<"Enter 0 to stop the program"<<endl;
cin>>power;
}
return 0;
}
© Najwa Abd Ghafar – UiTM Johor
QUICK EXERCISE ©Rose Hafsah Ab Rauf – UiTM Shah Alam
- 1;
a. Draw a flowchart for the program above
b. What will be the output displayed on the screen if the numbers entered are
88, 89, 64 and 0 respectively?
c. Transform the program using a do..while loop
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
break STATEMENT
break Statement:
Causes an immediate exit from the enclosing loop
Exit the loop
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
What will be the output displayed on the screen for the program above?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
continue STATEMENT
continue Statement:
Causes the remaining statements in the current loop to be skipped
Then, proceed with the next loop
Go to next loop
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
What will be the output displayed on the screen for the program above?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
What is wrong with this source code? How can you fix this problem.
© Najwa Abd Ghafar – UiTM Johor
FLAG-CONTROLLED LOOP
©Rose Hafsah Ab Rauf – UiTM Shah Alam
Is used when the number of repetition (n) is NOT known in advance
BUT you want to use a Boolean variable to control the loop
For example:
The steps involved in writing flag-controlled loop:
1) Declare and initialize a CONTROL VARIABLE
2) Evaluate the CONTROL VARIABLE against the LOOP CONDITION
3) Update the CONTROL VARIABLE at each loop
(By storing TRUE or FALSE value)
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
a. Draw a flowchart for the program above
b. What will be the output displayed on the screen if the numbers entered are
2, 10, 8, 5 and 0 respectively?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
NESTED LOOP
Nested Loop:
Is used when you want to execute a loop within another loop
An inner loop will be contained inside an outer loop
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
What will be the output displayed on the screen for the program above?
© Najwa Abd Ghafar – UiTM Johor
QUICK EXERCISE
©Rose Hafsah Ab Rauf – UiTM Shah Alam
What will be the output displayed on the screen for the program above?
© Najwa Abd Ghafar – UiTM Johor
QUICK EXERCISE
©Rose Hafsah Ab Rauf – UiTM Shah Alam
What will be the output displayed on the screen for the program above?