1
CSC128 FUNDAMENTALS OF COMPUTER
PROBLEM SOLVING
TOPIC 4 – REPETITION
CONTROL STRUCTURE
2
Repetition Control Structure
3
REPETITION CONTROL STRUCTURE
✓ 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
4
✓To learn about repetition (looping) control structures
5
6
CONTROL STRUCTURES
Computer program can be executed:
i. In Sequence
ii. By Selection (Branch): Making a choice
iii. By Repetition (Iterative): Looping
statement1
expression
statement1
expression statement
statementN statement2 statement1
a. Sequence b. Selection c. Repetition
7
REPETITION CONTROL 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 expression statement
✓ An ITERATION
Repetition Control
Structure
8
WHY WE
NEED “LOOP/
ITERATION”?
Write a program that
will display the word
“Hello” 100 times.
9
WHY “LOOP / ITERATION”?
Use repetition
(looping) to
repeat the same
statements over
and over again
× IMPRACTICAL ✓PRACTICAL
10
WHY “LOOP / ITERATION”?
REAL LIFE SITUATION:
You're expecting an email (doesn't reach your mailbox).
while(you haven't received the email)
{
Refresh inbox to check if it's there.
Repeat next day/next hour.
}
11
WHY “LOOP / ITERATION”?
REAL LIFE SITUATION:
You make a workout regime for a month in January.
int gymWorkout;
for(gymWorkout =1; gymWorkout <=31; gymWorkout++)
workout();
12
REPETITION (LOOPING) STRUCTURE
There are THREE (3) types of To write a program for each type
repetition structure: of repetition structure in C++, you
could use any of these:
1. Counter-controlled Loop 1. while loop
2. Sentinel-controlled Loop 2. for loop
3. Flag-controlled Loop 3. do...while loop
13
✓Explore how to construct and use counter-controlled loop,
sentinel-controlled loop and flag-controlled loop
14
TYPES OF
REPETITION
STRUCTURE
COUNTER-CONTROLLED
LOOP
15
COUNTER-CONTROLLED LOOP
• 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:
✓Displaying the word “Hello” 10 times
✓Getting input from user 5 times
✓Repeating a process 3 times
16
COUNTER-CONTROLLED LOOP
THREE (3) steps involved in writing counter-controlled loop:
Step Declare and initialize a LOOP CONTROL
1 VARIABLE (LCV). Before entering the loop.
Step Evaluate the LCV against the LOOP CONDITION
2 (To check whether the looping should continue or
not)
Step Update the LCV at each loop
3 (By incrementing or decrementing)
17
COUNTER-CONTROLLED LOOP
REAL LIFE SITUATION:
During Eid Festive, you make a visit to your neighbour’s house.
ADAB TO FOLLOW when visiting your neighbour’s house:
“Give Salam until 3 times, if no response, leave the house.”
int giveSalam = 0;
while(giveSalam < 3)
{
Knock your neighbour’s door.
Shout “Assalamualaikum”.
giveSalam = giveSalam + 1; //giveSalam++;
}
18
Suggested Answer:
A variable whose value determines whether the loop body will be
executed or not.
19
COUNTER-
CONTROLLED
LOOP
while loop
20
while LOOP
SYNTAX:
while(expression)
{
statement; expression statement
}
21
EXAMPLE 1
1. Draw a flowchart for the above code segment.
2. What will be the output displayed on the screen
for the above code segment?
3. What is the purpose of the above code segment?
22
PRACTICE 1.1
What will be the output displayed on the screen for
the above code segment?
23
PRACTICE 1.2
What will be the output displayed on the screen for
the above code segment?
24 EXERCISE 1
1. Draw a
flowchart.
2. What will be the
output displayed
on the screen if
the numbers
entered are 5, 2,
10, 6 and 15
respectively?
25
EXAMPLE 2
PART B: QUESTION 4 a (i)
26
PRACTICE 2
27 EXERCISE 2
Identify the following
components in
repetition control
structure:
i. Loop control
variable (LCV)
ii. Initialization
statement
iii.Loop continuation
condition
28 PRACTICE 3
QUESTION
Correct the
following code so
that it reads and
finds the sum of
30 numbers.
29 PRACTICE 3
ANSWER
Correct the
following code so
that it reads and
finds the sum of
30 numbers.
30
EXAMPLE 3
ANSWER
12 18 6
31
EXERCISE 3
ANSWER
6 12 12
32
EXAMPLE 4
PART B: QUESTION 4 c
Suggested Answer:
int n = 0;
cout << "N\t\t4^N"<<endl;
cout << "_______________________\n";
while (n < 4)
{
cout <<n <<"\t\t";
cout <<(pow(4, n)) <<endl;
n++;
}
33
COUNTER-
CONTROLLED
LOOP
do…while
loop
34
do…while LOOP
SYNTAX:
do
statement
{
statement; expression
}
while(expression);
semicolon
35
do…while LOOP
REAL LIFE SITUATION:
Your mom asked you to water her plant until it start flowering.
“Remember to water my plant, if it start flowering, stop watering.”
int water = 0;
bool isFlowering = false;
do
waterPlant()
{
Water mom’s plant. water++
water++;
Check if start flowering?
{if yes, isFlowering = true;} Is the plant start
flowering yet?
}while(isFlowering == false);
cout << “Mom, your plant start flowering.\n I
water it for ”<<water <<“times.” Tell mom:
Her plant start flowering
How many times you water her plant
36
while VS. do…while LOOP
while STRUCTURE: do…while STRUCTURE:
initialize LCV; initialize LCV;
while (loop condition) do
{ {
statement; statement;
update LCV; update LCV;
} } while(loop condition);
• The loop condition is • The loop condition is
evaluated at the beginning evaluated at the end of
of loop loop
• The loop can be executed • The loop must be executed at
0 or more times least once (1 or more times)
37
EXAMPLE 5
i. If count is initialized as 6,
what would be the output
displayed on the screen?
ii. Transform the program by
using do.. while loop
instead of while loop
iii. Using the new program, if
count is initialized as 6,
what would be the output
displayed on the screen?
38
EXERCISE 4
i. Draw a flowchart for the
program.
ii. What will be the output
displayed on the screen
if the salary entered are
2000, 1500, 1200, 1000
and 2500 respectively?
39
OCT 2016 EXAMPLE 6
ANSWER:
50 //0.5m
85 //0.5m
105 //0.5m
//0.5m format
40
SEP 2015 EXERCISE 5
ANSWER:
100 //0.5m
200 //0.5m
300 //0.5m
400 //0.5m
41
COUNTER-
CONTROLLED
LOOP
for loop
42
for LOOP
SYNTAX:
initial statement
update statement
statement
Loop condition
43
for LOOP
REAL LIFE SITUATION:
During Eid Festive, you make a visit to your neighbour’s house.
ADAB TO FOLLOW when visiting your neighbour’s house:
“Give Salam until 3 times, if no response, leave the house.”
int giveSalam;
for(giveSalam = 0; giveSalam < 3; giveSalam++)
{
Knock your neighbour’s door.
Shout “Assalamualaikum”.
}
cout << “We already gave salam for ” <<giveSalam <<“ times.”;
44
EXAMPLE 7
PART B: QUESTION 4 a (ii)
45 PRACTICE 4
Transform the
program using
a for loop
instead of
while loop
46 PRACTICE 5
i. Draw a flowchart for the above program.
ii. What will be the output displayed on the screen
for the program?
47
EXERCISE 6
PART B: QUESTION 4 b
48
OCT 2016 EXAMPLE 8
ANSWER:
@@@ //0.5m
### //0.5m
@@@ //0.5m
### //0.5m
49
SEP 2015 EXERCISE 7
ANSWER:
19 //0.5m
22 //0.5m
25 //0.5m
28 //0.5m
50
QUICK TIP
51
QUICK TIP
52
TYPES OF
REPETITION
STRUCTURE
*SENTINEL-CONTROLLED
LOOP
*Sentinel = a soldier or
guard whose job is to
stand and keep watch.
53
SENTINEL-CONTROLLED LOOP
• 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)
54
SENTINEL-CONTROLLED LOOP
THREE (3) steps involved in writing sentinel-controlled loop:
Step Declare and initialize a LOOP CONTROL
1 VARIABLE (LCV). Before entering the loop.
Step Evaluate the LCV against the LOOP CONDITION
2
Step Update the LCV at each loop
3 (By input received from user)
55
EXAMPLE 9
1 i. Draw a flowchart for
the program
ii. What will be the
output displayed on
2 the screen if the
numbers entered are
88, 89, 64 and -1
respectively?
3
iii. Transform the
program using a
do...while loop
56 PRACTICE 6
Refer your manual, page 99:
Do question 5
while loop
57 PRACTICE 7
Refer your manual, page 99:
Do question 5
do…while loop
58
TYPES OF
REPETITION
STRUCTURE
FLAG-CONTROLLED
LOOP
59
FLAG-CONTROLLED LOOP
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:
✓ 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)
60
FLAG-CONTROLLED LOOP
THREE (3) steps involved in writing flag-controlled loop:
Step Declare and initialize a LOOP CONTROL
1 VARIABLE (LCV). Before entering the loop.
Step Evaluate the LCV against the LOOP CONDITION
2
Step Update the LCV at each loop
3 (By storing TRUE or FALSE value)
61 PRACTICE 8
Refer your manual, page 99:
Do question 5
1
while loop
2
3
62 PRACTICE 9
1
i. Draw a flowchart for the program
3 ii. What will be the output displayed on the
screen if the numbers entered are 2, 10,
8, 5 and 0 respectively?
63
EXAMPLE 10
PART B: QUESTION 3 b (II)
64
EXERCISE 8
PART B: QUESTION 4
65
PRACTICE 10
PART A: QUESTION 8
66
PART B: QUESTION 4
EXAMPLE 11
3 marks
67
EXERCISE 9
68
✓Discover how to form and use nested repetition
control structures
69
NESTED LOOP
• Is used when you want
to execute a loop
within another loop
• An inner loop will be
contained inside an
outer loop
70
EXAMPLE 12
What will be the output displayed on the
screen for the above program?
71
EXAMPLE 12
Memory i <= 3
i=1 √
i=2 √
i=3 √
memory j <= i Screen
j=1 √
j=2
j=3
√
√
*
What will be the output displayed on the **
screen for the above program?
***
72
EXERCISE 10
What will be the output displayed on the screen
for the above program?
73 PART A: QUESTION 8
EXERCISE 10
Memory i <= 3
74
EXAMPLE 13 i=1 √
i=2 √
i=3 √
i=4 √
i=5 √
Memory j<5
j=1 √
i=2 √
i=3 √
i=4 √
memory k<i Screen
k=0 √ ----*
k=1 √
k=2 √
---**
k=3 --***
What will be the output displayed on k=4 -****
the screen for the above program? k=5 *****
75
PRACTICE 11
PART A: QUESTION 7
76
PRACTICE 12
PART B: QUESTION 4
(3 marks)
77
PRACTICE 13
PART B: QUESTION 3
78
break
STATEMENT
• Causes an IMMEDIATE
EXIT from the
enclosing loop
79
break STATEMENT
• Causes an IMMEDIATE
EXIT from the
enclosing loop
80
PRACTICE 14
What will be the output displayed on the screen
for the above program?
81
PART B: QUESTION 3 (b)
EXAMPLE 14
82
continue
STATEMENT
• Causes the remaining
statements in the
current loop to be
skipped, then, proceed
with the next loop.
83
continue STATEMENT
• Causes the remaining
statements in the
current loop to be
skipped, then, proceed
with the next loop.
84
PRACTICE 15
What will be the output displayed on the screen
for the above program?
85
REFERENCES
Malik, D.S (2010). “C++ Programming: From Problem Analysis to
Program Design”. Cengage Learning
Najwa Abd Ghafar. (2018). CSC128: Chap 4 - Repetition Control
Structure [PowerPoint slides in PDF].
What are the best real world analogies to explain loops in programming?
https://www.quora.com/What-are-the-best-real-world-analogies-to
explain-loops-in-programming
Refresh in GIF. Retrieved at
https://cloud.lovindublin.com/images/uploads/2015/12/_blogWide/refresh
.gif?mtime=20151206163411
Gym in GIF. Retrieved at https://www.showpo.com/showpoedit/fit-af/the-
seven-stages-of-a-gym-membership/
86
TOPIC 4 – REPETITION CONTROL
STRUCTURE
COMPLETE