Chapter 4 Control Structure: Loop: Prof. Kuanquan Wang
Chapter 4 Control Structure: Loop: Prof. Kuanquan Wang
Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure
1
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
Sequence
Selection
Repetition or loop
Counter Controlled 1, 2, 3, 4, , 4, 3, 2, 1
C Programming Language
Step x
Update counter Step n
C Programming Language
Step x
Update counter Step n
C Programming Language
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
C Programming Language 7
start
Input n1 n1 n2 n3 n4 n5 sum 2 3 4 5 6 20
This flowchart does not use loop, hence we need to use 6 different variables
C Programming Language
end
counter 1, sum 0
false
counter sum
1 2 3 4 5 6 14 20 0 2 5 9
14 0+ 2 5 9 +5 2 3 4 6
2 3 4 5 6
counter > 0
true
Loop : for
Condition is tested first Loop is controlled by a counter Syntaxes for (initial value ; condition; update counter) statement;
Or
i 0, sum 0
false
i< 5
true
input x sumsum+ x
i++
output sum
C Programming Language
int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(%d,&x); sum = sum + x; } printf(%d,sum);
12
for statement
???
num
C Programming Language
13
for statement
1
num
C Programming Language
14
for statement
1
num
C Programming Language
15
for statement
1
num
C Programming Language
16
for statement
2
num
C Programming Language
17
for statement
2
num
C Programming Language
18
for statement
2
num
C Programming Language
19
for statement
3
num
C Programming Language
20
for statement
3
num
C Programming Language
21
for statement
3
num
C Programming Language
22
for statement
4
num
C Programming Language
23
for statement
4
Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num); printf(have come to exit\n);
num
C Programming Language
24
for statement
4
Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num); printf(have come to exit\n);
num
C Programming Language
25
Loop Structure
C Programming Language
26
condition
true
condition
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
condition
false
Step n
C Programming Language 30
condition
false
Step n
C Programming Language 31
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
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
Condition-Controlled Loop
m 110 57 5
WRONG INPUT
true
110 5 57 << 0 < 0 || 0 || 5 || 57 >100 110 >100 >100
m<0 || m>100
false
Condition-controlled 5 > 49 57 > 49 loop with its condition being tested at the end
true
m>49
false
PASS
FAIL
C Programming Language
34
input m
false
m<0 || m>100
true
WRONG INPUT
input m
Condition-controlled loop with its condition being tested first
C Programming Language
true
m>49
false
PASS
FAIL
35
int marks; scanf(%d,&marks); while (marks<0) | | (marks>100) { printf(WRONG INPUT); scanf(%d,&marks); } if (marks>49) { printf(PASS); else printf(FAIL); }
C Programming Language
Double Selection
36
do-while statement
??? 65 66 67 68
??? 67
start
end
Example : printf(Input start and end value : ); scanf(%d %d, &start, &end); do { printf(%c (%d)\n, start, start); start++; 66 <= 67 67 } while (start <= end) ; 68
_ Input start and end value : 65 _ 67 67_ A (65) _ (66) B _ (67) C Programming Language C
_
37
Can you Input: A set of identify integers the ending with a input and negative integer or a zero output???
38
Sentinel Value
C Programming Language
39
C Programming Language
40
Try to understand
sum0
input x
false
x>0
true
input x sumsum+x
?
41
Exercise
Given a set of integers with the last one being 999 Display the summation of all the integers.
Sentinel-controlled loop
sum=0 input x
false #include <stdio.h> void main() { int sum, x; sum = 0; scanf(%d, &x); while (x != 999) {
x!=999
true
sum = sum + x;
scanf(%d, &x); }
int sum, x; sum = 0; scanf(%d, &x); 3!= 999 != != 999 999 999 != 999 while (x != 999) { 123 sum = sum + x; scanf(%d, &x); } printf(\nThe sum : %d\n, sum);
? 999 23 1 3
sum
_ 1
23
999
44
The sum : 27
C Programming Language
3 statements in C language
for, while, do while