Chapter 4 Control Structure: Loop - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Chapter 4 Control Structure: Loop

Description:

Title: Control Structure: Loop (Part 1) Author: Norleyza Last modified by: Owner Created Date: 6/29/2004 3:43:40 AM Document presentation format – PowerPoint PPT presentation

Number of Views:170
Avg rating:3.0/5.0
Slides: 46
Provided by: Norl151
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4 Control Structure: Loop


1
Chapter 4 Control Structure Loop
Prof. Kuanquan Wang The School of Computer
Science and Technology Harbin Institute of
technology
  • Knowledge
  • Understand the various concepts of loop control
    structure
  • Skill
  • Be able to develop a program involving loop
    control structure

1
2
Outline
  • 4.1 Review and introduction to loop flow
    control structure
  • 4.2 Loop control with counter
  • 4.3 Loop control with condition test
  • 4.4 Loop control with sentinel

3
4.1 Review and introduction to loop flow control
structure
How many basic flow control structure?
There are 3 kinds of basic flow control structure.
Sequence
Selection
Repetition or loop
4
How Loops are Controlled?
5
4.2 Counter Controlled Loop
counter ? initial Value
false
test counter value
true
Step x
Update counter
Step n
6
Counter Controlled Loop
counter initialValue
counter ? initial Value
false
test counter value
true
Step x
Update counter
Step n
7
Example
  • 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
8
Assume input example 2 3 4 5 6
start
Input n1
Input n2
This flowchart does not use loop, hence we need
to use 6 different variables
Input n3
input n4
input n5
sum ? n1n2n3n4n5
output sum
end
9
Counter-Controlled Loop
Assume input example 2 3 4 5 6
1
2
3
4
5
6
counter ? 1, sum ? 0
counter
0
2
5
9
14
20
sum
0 2
2 3
5 4
9 5
14 6
counter lt 6
false
1 lt 6 true
2 lt 6 true
3 lt 6 true
4 lt 6 true
5 lt 6 true
6 lt 6 false
true
input n
2
3
4
5
6
n
sum ? sum n
This loop is counter-controlled
The counter Increases by 1
Uses only 3 variables
counter
output sum
10
Decreasing Counter-Controlled Loop
counter ? 5, sum ? 0
false
counter gt 0
true
input x
sum?sum x
counter--
output sum
11
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

C Programming Language
11
12
i ? 0, sum ? 0
false
i lt 5
int x, sum, i sum 0 for (i 0 i lt 5 i)
scanf(d,x) sum sum x
true
input x
sum?sum x
i
printf(d,sum)
output sum
C Programming Language
12
13
for statement
???
  • Example
  • for ( num 1 num lt 3 num )
  • printf(d\t, num)

num
1 _
printf(have come to exit\n)
_
C Programming Language
13
14
for statement
1
  • Example
  • for (num 1 num lt 3 num )
  • printf(d\t, num)

num
printf(have come to exit\n)
_
C Programming Language
14
15
for statement
1
Example for (num 1 num lt 3 num )
printf(d\t, num)
num
printf(have come to exit\n)
_
C Programming Language
15
16
for statement
1
  • Example
  • for (num 1 num lt 3 num )
  • printf(d\t, num)

num
printf(have come to exit\n)
1 _
C Programming Language
16
17
for statement
2
  • Example
  • for (num 1 num lt 3 num )
  • printf(d\t, num)

num
printf(have come to exit\n)
1 _
C Programming Language
17
18
for statement
2
  • Example
  • for (num 1 num lt 3 num )
  • printf(d\t, num)

num
printf(have come to exit\n)
1 _
C Programming Language
18
19
for statement
2
  • Example
  • for (num 1 num lt 3 num )
  • printf(d\t, num)

num
printf(have come to exit\n)
1 2 _
C Programming Language
19
20
for statement
3
  • Example
  • for (num 1 num lt 3 num )
  • printf(d\t, num)

num
printf(have come to exit\n)
1 2 _
C Programming Language
20
21
for statement
3
  • Example
  • for (num 1 num lt 3 num )
  • printf(d\t, num)

num
printf(have come to exit\n)
1 2 _
C Programming Language
21
22
for statement
3
  • Example
  • for (num 1 num lt 3 num )
  • printf(d\t, num)

num
printf(have come to exit\n)
1 2 3 _
C Programming Language
22
23
for statement
4
  • Example
  • for (num 1 num lt 3 num )
  • printf(d\t, num)

num
printf(have come to exit\n)
1 2 3 _
C Programming Language
23
24
for statement
4
  • Example
  • for (num 1 num lt 3 num )
  • printf(d\t, num)
  • printf(have come to exit\n)

num
1 2 3 _
C Programming Language
24
25
for statement
4
  • Example
  • for (num 1 num lt 3 num )
  • printf(d\t, num)
  • printf(have come to exit\n)

num
1 2 3 have come to exit_
C Programming Language
25
26
Loop Structure
4.3 Loop control with condition test
C Programming Language
26
27
Testing Condition First
false
true
C Programming Language
27
28
Testing Condition First
Step a
condition
Step x
Step y
Step n
C Programming Language
28
29
Loop while
  • Condition is tested first
  • Loop is controlled by condition or a counter
  • Syntax
  • while (condition)
  • statement
  • Or
  • while (condition)
  • statement
  • statement

C Programming Language
29
30
Testing condition later
Step a
Step x
Step y
true
condition
false
Step n
C Programming Language
30
31
Testing condition later
Step a
Step x
Step y
true
condition
false
Step n
C Programming Language
31
32
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

C Programming Language
32
33
  • 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

C Programming Language
33
34
Condition-Controlled Loop
Assume m110
Assume m5
Assume m57
110
5
57
m
input m
WRONG INPUT
true
mlt0 mgt100
110 lt 0 110 gt100
5 lt 0 5 gt100
57 lt 0 57 gt100
false
true
Condition-controlled loop with its condition
being tested at the end
mgt49
5 gt 49
57 gt 49
PASS
false
FAIL
WRONG INPUT
FAIL
PASS
C Programming Language
34
35
input m
false
mlt0 mgt100
true
WRONG INPUT
input m
Condition-controlled loop with its condition
being tested first
true
mgt49
PASS
false
FAIL
C Programming Language
35
36
int marks scanf(d,marks) while (markslt0)
(marksgt100) printf(WRONG INPUT) scanf(d
,marks) if (marksgt49) printf(PASS) else
printf(FAIL)
Double Selection
C Programming Language
36
37
do-while statement
???
???
65
67
66
67
68
start
end
  • Example
  • printf(Input start and end value )
  • scanf(d d, start, end)
  • do
  • printf(c (d)\n, start, start)
  • start
  • while (start lt end)

66 lt 67
67 lt 67
68 lt 67
_
Input start and end value _
Input start and end value 65 67_
Input start and end value 65 67 A (65) _
Input start and end value 65 67 A (65) B (66) _
Input start and end value 65 67 A (65) B (66) C
(67) _
C Programming Language
37
38
4.4 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

Input A set of integers ending with a negative
integer or a zero
Output Summation and Average of these integers
39
  • Input Example
  • 30 16 42 -9
  • Output Example
  • Sum 88
  • Average 29.33

Sentinel Value
40
(No Transcript)
41
What will happen if this statement is deleted???
?
Try to understand
sum?0
input x
What happened if these 2 statements exchange
places
false
xgt0
true
sum?sumx
input x
?
input x
sum?sumx
display sum
42
Exercise
  • Given a set of integers with the last one being
    999
  • Display the summation of all the integers.
  • Input example
  • 1 3 23 999
  • Output example
  • Sum 27

Draw the flowchart for this problem
C Programming Language
42
43
Sentinel-controlled loop
include ltstdio.hgt void main() int sum, x
sum 0 scanf(d, x) while (x !
999) sum sum x scanf(d,
x) printf(The sum d\n, sum)
true
C Programming Language
43
44
int sum, x sum 0 scanf(d,
x) while (x ! 999) sum sum x
scanf(d, x) printf(\nThe sum
d\n, sum)
1
3
23
999
1 ! 999
3 ! 999
23 ! 999
999 ! 999
0
01
1
13
4
423
27
_
1
1 3
1 3 23
1 3 23 999
The sum 27
C Programming Language
44
45
  • The concept of Loop Structure
  • 3 methods to control loop structure
  • Counter Controlled, Condition Controlled,
    Sentinel Controlled
  • 3 statements in C language
  • for, while, do while

Thank you for your attention! Now let us have a
break!
C Programming Language
45
Write a Comment
User Comments (0)
About PowerShow.com