Sie sind auf Seite 1von 53

Module 5: Repetition Control

Structures

CSPROG1 Introduction to Programming


Languages
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
First
Step x

Step y

true
condition

false
Step n
Testing Condition Step a
First
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 test counter false
Loop value
true
Step x

Update counter

Step n
counter initialValue
Counter Controlled
test counter false
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 2
Input n1 n1
Input n2 n2 3
This flowchart
does not use Input n3
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
Counter count 654321
Controlle counter 1, sum 0 er su 14
20 9520 14 0 ++5
2
5
9 2
3
4
d Loop m
65
14<<<666 false
2
3
6
true
true
counter < 6 false
true
input n n 65432
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
Decreasing counter > 0

Counter true

input x
Controlled
Loop sumsum+ 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
57<<0<0||0||5||57
110
5 >100
110
>100 m<0 || m>100
>100 false
Condition-
557>>
49
controlled loop true
49 m>49 PASS
with its condition
false
being tested at
WRONG
FAIL
PASS the INPUT
end 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 integersending and
with a
negative integer or a zero
display the summation and average of these
integers.
A negative or zero input indicateOutput:
theSummation
end ofand
input process Average of these integers
Input Example:
30 16 42 -9

Output Example:
Sum = 88
Average = 29.33
What will
Try to understand happen if this
sum0 ?
statement is
deleted???
input x
What happened
false
if these 2
x>0
statements
true exchange
places
sumsum+x
input x

input x
sumsum+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

sumsum+ x

counter++

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

input x
How many times sumsum+ x
does the loop get
executed? i++

output sum
i 0, sum 0

false
i< 6
true

input x
How many times sumsum+ x
does the loop get
executed? i++

output sum
i 0, sum 0

false
i< 5
true

input x
How many times sumsum+ 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++) {
sumsum+ x
cin>>x;
sum = sum + x;
i++ }
output sum
cout<<sum;
for statement ???
num
Example:
for ( num = 1; num <= 3; num++ )
cout<<num;
1 _

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

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

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

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

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

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

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

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

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

1 2 3 _
for statement 4
num
Example:
for (num = 1; num <= 3; num++ )
cout<<num<< ;

1 2 3 _
for statement 4
num
Example:
for (num = 1; num <= 3; num++ )
cout<<num<< ;

1 2 3 _
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
flowchart to a m>49 PASS
C++ program false

FAIL
int marks;
cin>>marks;
while (marks<0) | | (marks>100) {
Double
cout<<WRONG INPUT;
Selection
cin>>marks;
}
if (marks>49) {
cout<<PASS;
else
cout<<FAIL;
}
Exercise
Given a set of integers with the last one being
999 Draw the
Display the summation of all the integers.
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;
sumsum+x
cin>>x;
}
input x
cout<<The sum : <<sum;

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

1_ 3 23 999

The sum : 27
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) ; 68
66 <= 67
67
67
_Input start and end value : 65
_ 67
67_
65
_
66
_
67
_
0000is
isisan
is _
aneven
an
an evennumber.
even
even number.
number._
number.
number. Printififififeven
Print
Print
Print even!!! !
even
even
222_is
is
isan
an
aneven
even number. Print
even number.
number._Print ifif even
even !
continue statement
Example: 44
_isis an
an even
even number._
number. Print if even !

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

break statement 1
2
3YeePee! Im 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
3
4 43
21<=
<=
<= 7777 24
31==
== 44 true
value
cout<<YeePee! Im 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 using
For loop
Do while loop
While loop

Das könnte Ihnen auch gefallen