Topic 4 Repetition Control Structure
Topic 4 Repetition Control Structure
statement1
expression
statement1
expression statement
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:
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:
TYPES OF
REPETITION
STRUCTURE
COUNTER-CONTROLLED
LOOP
15
COUNTER-CONTROLLED LOOP
Suggested Answer:
COUNTER-
CONTROLLED
LOOP
while loop
20
while LOOP
SYNTAX:
while(expression)
{
statement; expression statement
}
21
EXAMPLE 1
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);
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
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.
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.
3
62 PRACTICE 9
1
PART B: QUESTION 4
65
PRACTICE 10
PART A: QUESTION 8
66
PART B: QUESTION 4
EXAMPLE 11
3 marks
67
EXERCISE 9
68
NESTED LOOP
• Is used when you want
to execute a loop
within another loop
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
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