Académique Documents
Professionnel Documents
Culture Documents
Loop
(Part 1)
Knowledge:
Understand the various concepts of loop control structure
Skill:
Be able to develop a program involving loop control structure
Loop Structure
TK1913-C Programming
Testing
Condition First
Step a
false
condition
true
Step x
Step y
Step n
TK1913-C Programming
Testing
Condition First
Step a
condition
Step x
Step y
Step n
TK1913-C Programming
Testing condition
later
Step a
Step x
Step y
true
condition
false
Step n
TK1913-C Programming
Testing condition
later
Step a
Step x
Step y
true
condition
false
Step n
TK1913-C Programming
Condition
Controlled
TK1913-C Programming
Sentinel
Controlled
Counter Controlled
1, 2, 3, 4,
, 4, 3, 2, 1
7
Step x
Update counter
Step n
TK1913-C Programming
Step x
Update counter
Step n
TK1913-C Programming
Example:
Draw a flowchart for the
following problem:
Read 5 integer and display the
value of their summation.
Can you
identify the
input and
output???
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
TK1913-C Programming
10
start
n1
Input n2
n2
Input n3
n3
input n4
n4
n5
sum
20
Input n1
This flowchart does
not use loop, hence
we need to use 6
different variables
input n5
sum n1+n2+n3+n4+n5
output sum
TK1913-C Programming
end
11
CounterAssume input
Controlled
example:
2 3 4 5 6
Loop
counter 1, sum 0
1
2
3
4
5
6
counter
14
20
0
2
5
9
sum
false
counter < 6
65
14<<<666
2
3
14
0 ++ 5
2
5
9
26
3
4
false
true
true
true
input n
2
3
4
5
6
sum sum + n
counter++
This
loop
Uses
only is
The
counter
counter-controlled
3 variables
Increases
by 1
output sum
TK1913-C Programming
12
counter > 0
true
input x
sumsum+ x
counter-output sum
TK1913-C Programming
13
TK1913-C Programming
14
Assume
m=110
m=57
m=5
Condition-Controlled Loop
m
110
57
5
input m
WRONG INPUT
true
110
5
57
<<0<0||0||5||57
>100
110
>100
>100
m<0 || m>100
false
557
>Condition-controlled
49
> 49
loop with its condition
being tested at the end
true
m>49
PASS
false
FAIL
WRONG INPUT
FAIL
PASS
TK1913-C Programming
15
input m
false
m<0 || m>100
true
WRONG INPUT
input m
Condition-controlled
loop with its
condition being
tested first
TK1913-C Programming
true
m>49
PASS
false
FAIL
16
Sentinel-Controlled Loop
Draw a flowchart for a
problem which:
Receive a number of
positive integers and
display the summation
and average of these
integers.
A negative or zero input
indicate the end of input
process
TK1913-C Programming
Can you
Input: A set of identify
integers the
ending with a
input and
negative integer or a zero
output???
17
Input Example:
30 16 42 -9
Sentinel
Value
Output Example:
Sum = 88
Average = 29.33
TK1913-C Programming
18
Now
What have you
understand?
TK1913-C Programming
19
Try to understand
What will
happen if this
statement is
deleted???
sum0
input x
false
x>0
true
input x
sumsum+x
input x
sumsum+x
What happened
if these 2
statements
exchange
places
display sum
TK1913-C Programming
20