You are on page 1of 53

Module 10.

1: Repetition Control
Structures

Introduction to Programming
Objectives
Upon completion of this module, you will be able to:
• Know the loop structure
• Know the different ways on how the loop is controlled
• Simulate a loop statement to determine the output
Loop Structure

Condition is tested first Condition is tested later


Testing Condition Step a
First
false
condition

true
Step x

Step y

Step n
Testing Condition Step a
First
condition

Step x

Step y

Step n
Testing Condition Step a
Last
Step x

Step y

true
condition

false
Step n
Testing Condition Step a
Last
Step x

Step y

true
condition

false
Step n
How Loops are Controlled

Condition Sentinel
Controlled Controlled
Counter Controlled
•1, 2, 3, 4, …
•…, 4, 3, 2, 1
counter ← initialValue
Counter Controlled false
test counter
Loop value

true
Step x

Update counter

Step n
counter ← initialValue
Counter Controlled false
test counter
Loop value

true
Step x

Update counter

Step n
Example: Identify I/O
Draw a flowchart for the following problem:
Read 5 integer and display the value of their summation.

Input : 5 integer
n1, n2, n3, n4, n5
Output: The summation of
n1, n2, .., n5

Input example: 2 3 4 5 6

Output example: 20
Assume input example: start
2 3 4 5 6
Input n1 n1 2

Input n2
n2 3
This flowchart
Input n3
does not use
n3 4
loop, hence we input n4
need to use 6 n4 5
input n5
different
variables sum ← n1+n2+n3+n4+n5 n5 6

output sum su 20
m
end
count 1
2
3
4
5
6
Counter
counter ← 1, sum ← 0 er su 14
20
0 14
2
5
9 0 ++5
2
5
9 2
3
4
Controlled Loop
m 6
false 65
14<<<666 false
2
3 true
true
counter < 6
true
input n n 2
3
4
5
6
Assume input
example: sum ← sum + n
2 3 4 5 6

counter++ This loop


Uses
The only is
counter
output sum counter-controlled
3 variables
Increases by 1
counter ← 5, sum ← 0

false
counter > 0

Decreasing true
Counter
Controlled input x
Loop

sum←sum+ x

counter--

output sum
• Example: Draw a flowchart for this problem;
• Given an exam marks as input, display the
appropriate message based on the rules below:

– If marks is greater than 49, display “PASS”,


otherwise display “FAIL”
– However, for input outside the 0-100 range, display
“WRONG INPUT” and prompt the user to input
again until a valid input is entered
Assume
m=110
m=57
m=5
m 110
57
5
input m

“WRONG INPUT”

true
m<0 || m>100
57
5 <<0<0||0||5||57
110 >100
>100
110
>100 false
Condition-
557>>49
controlled loop true
49 m>49 “PASS”
with its condition
false
being tested at
WRONG
FAIL
PASS the end
INPUT “FAIL”
input m

false
m<0 || m>100

true
“WRONG INPUT”

input m

Condition-controlled true
loop with its m>49 “PASS”
condition being false
tested first “FAIL”
Sentinel-Controlled Loop
• Draw a flowchart for a problem which:
Input: A set of integers
• Receive a number of positive integers
ending and
with a
negative integer or a zero
display the summation and average of these
integers.
• A negative or zero input indicate the end of
Output: Summation and
input process Average of these integers
• Input Example:
• 30 16 42 -9

• Output Example:
• Sum = 88
• Average = 29.33
What will
Try to understand
sum←0 ?
happen if this
statement is
deleted???
input x

What happened if
false these 2 statements
x>0 exchange places

true
input x
sum←sum+x

input x
sum←sum+x ?
display sum
Loop : for
 Condition is tested first
 Loop is controlled by a counter
 Syntaxes
for (initial value ; condition; update counter)
statement;
Or
for (initial value ; condition; update counter) {
statement;
statement;
}
Example
• Write a program which does the following:
• Reads 5 integers and displays the sum of all
• integers

• Input example: 3 6 4 1 2
• Output example: 16
counter ← 1, sum ← 0

false
counter < 6

true
Recall the flowchart input x

sum←sum+ x

counter++

output sum
i ← 1, sum ← 0
Note the initial
value of i
and condition false
i< 6

true
input x

How many times sum←sum+ x


does the loop get
executed? i++

output sum
i ← 0, sum ← 0

false
i< 6

true
input x

How many times sum←sum+ x


does the loop get
executed? i++

output sum
i ← 0, sum ← 0

false
i< 5

true
input x

How many times sum←sum+ x


does the loop get
executed? i++

output sum
The C++ statements:

int x, sum, i;
sum = 0;
for (i = 0; i < 5; i++) {
cin>>x;
sum = sum + x;
}
cout<<sum;
i ← 0, sum ← 0

false
i< 5 int x, sum, i;
true sum = 0;
input x for (i = 0; i < 5; i++) {
sum←sum+ x
cin>>x;
sum = sum + x;
i++ }
output sum
cout<<sum;
for statement ???
num
• Example:
• for ( num = 1; num <= 3; num++ )
• cout<<num;
1 _

Introduction to Computer Programming


for statement 1
num
• Example:
• for (num = 1; num <= 3; num++ )
• cout<<num<<“ “;

Introduction to Computer Programming


for statement 1
num
• Example:
• for (num = 1; num <= 3; num++ )
• cout<<num<<“ “;

Introduction to Computer Programming


for statement 1
num
• Example:
• for (num = 1; num <= 3; num++ )
• cout<<num<<“ “;

1 _

Introduction to Computer Programming


for statement 2
num
• Example:
• for (num = 1; num <= 3; num++ )
• cout<<num<<“ “;

1 _

Introduction to Computer Programming


for statement 2
num
• Example:
• for (num = 1; num <= 3; num++ )
• cout<<num<<“ “;

1 _

Introduction to Computer Programming


for statement 2
bil
• Example:
• for (num = 1; num <= 3; num++ )
• cout<<num<<“ “;

1 2 _

Introduction to Computer Programming


for statement 3
num
• Example:
• for (num = 1; num <= 3; num++ )
• cout<<num<<“ “;

1 2 _

Introduction to Computer Programming


for statement 3
num
• Example:
• for (num = 1; num <= 3; num++ )
• cout<<num<<“ “;

1 2 _

Introduction to Computer Programming


for statement 3
num
• Example:
• for (num = 1; num <= 3; num++ )
• cout<<num<<“ “;

1 2 3 _

Introduction to Computer Programming


for statement 4
num
• Example:
• for (num = 1; num <= 3; num++ )
• cout<<num<<“ “;

1 2 3 _

Introduction to Computer Programming


for statement 4
num
• Example:
• for (num = 1; num <= 3; num++ )
• cout<<num<<“ “;

1 2 3 _

Introduction to Computer Programming


for
while
do-while
Loop: while
 Condition is tested first
 Loop is controlled by condition or a counter
 Syntax
while (condition)
statement;
Or
while (condition) {
statement;
statement;
}
Recall this example:
Given an exam marks as input, display the
appropriate message based on the rules below:
 If marks is greater than 49, display “PASS”,
otherwise display “FAIL”
 However, for input outside the 0-100 range,
display “WRONG INPUT” and prompt the
user to input again until a valid input is
entered
input m

false
m<0 || m>100

true
“WRONG INPUT”

input m

Exercise:
Convert this true
m>49 “PASS”
flowchart to a
C++ program false
“FAIL”
int marks;
cin>>marks;
while (marks<0) | | (marks>100)
Double
{
Selection
cout<<“WRONG INPUT”;
cin>>marks;
}
if (marks>49) {
cout<<“PASS”;
else
cout<<“FAIL”;
}
Exercise
• Given a set of integers with the last one being 999
• Display the summation of all the integers.Draw the
flowchart for
this problem
• Input example:
• 1 3 23 999

• Output example:
• Sum = 27
Sentinel-controlled loop
#include <iostream>
sum=0 using namespace std;
void main() {
input x int sum, x;

false sum = 0;

x!=999
cin>>x;
while (x != 999) {
true sum = sum + x;
sum←sum+x cin>>x;
}
input x cout<<“The sum : “<<sum;
}
output sum
int sum, x;
sum = 0;
cin>>x;
while (x != 999) {
sum = sum + x; x 999
23
1?
3
cin>>x;
123
3999
!=
!=!= !=
} 999
999
999
cout<<“The sum : “<<sum; su 4+23
0+1
1+3
27
0?
1
4
m

_
1 3 23 999

The sum : 27
Introduction to Computer Programming
Do-while Loop
• Statements in the loop are executed first (at
least once, and condition is tested last
• Loop is controlled by a condition or counter
• Syntax
• do {
• statement;
• statement;
• } while (condition);
• statement;
???
65
66
67
68 ???
67

start end
• Example :
• cout<<“Input start and end value : “;
• cin>>start; cin>>end;
• do {
• cout<< start;
• start++;
• } while (start <= end) ; 66 <= 67
67
68
67
_Input start and end value : _65 67 67_
65
_
66
_
67 Introduction to Computer Programming
_
0000is
is _aneven
isisan
an
an even
even number._
evennumber.
number.
number.
number. Printififififeven
Print
Print
Print even!!! !
even
even
222_is
is
isan
an
an even
even number. Print
even number.
number._Print ifif even
even !
continue statement
44
_ isis an
an even
even number._
number. Print if even !
Example:
for ( i = 0; i <= 5; i++ ) {
if ( i % 2 )
continue;
else
cout<<i<<“ is an even number. ”;
cout<<“Print if even ! \n”;
}
i i <= 5 i%2

0
1
5
6
4
3
2 64
05<=
1
2
3 <=
<=55
95 0
1
false
true
true
Input a value between
1-7: 5
_
4
1
break statement 2
3YeePee! I’m out of the
3
cout<<“Input a value between 1 –loop!
7: ”; ???
4
cin>>value;
for (i = 1; i <= 7; i++) { valu
e
if ( i = = value )
break;
cin>>endl>>i; i i <= i ==
} 1
2 43
3
4 21<=
<=
<=7777 4==
21
3 == 44 true
value
cout<<“YeePee! I’m out of the loop!\n”; true
true
true false
false
Exercises
• Create a program that will count from 1 to n, wherein
n is a user-input number.
• Create a menu and combine previously-created
programs into one. Follow the format below:
– Option 1 : Program 1
– Option 2 : Program 2
– Option 3 : Exit
The program must return to the main menu unless Option 3
was selected.

You might also like