460 likes | 621 Views
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. Outline.
E N D
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
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
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
Sentinel Controlled • Counter Controlled • 1, 2, 3, 4, … • …, 4, 3, 2, 1 Condition Controlled How Loops are Controlled? C Programming Language4
4.2 Counter Controlled Loop counter ← initial Value false test counter value true Step x Update counter Step n C Programming Language5
Counter Controlled Loop counter = initialValue counter ← initial Value false test counter value true Step x Update counter Step n C Programming Language6
Can you identify the input and output??? 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 Language7
20 6 2 5 3 4 n2 n1 n3 n5 sum n4 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 ← n1+n2+n3+n4+n5 output sum end C Programming Language8
Assume input example: 2 3 4 5 6 Counter-Controlled Loop 6 2 5 4 1 3 counter ← 1, sum ← 0 counter 20 14 5 2 9 0 sum 14 + 6 5 + 4 9 + 5 0 + 2 2 + 3 counter < 6 false 6 < 6 false 3 < 6 true 1 < 6 true 5 < 6 true 2 < 6 true 4 < 6 true true inputn 6 5 3 4 2 n sum ← sum + n This loop is counter-controlled The counter Increases by 1 Uses only 3 variables counter++ output sum C Programming Language9
Decreasing Counter-Controlled Loop counter ← 5, sum ← 0 false counter > 0 true input x sum←sum+ x counter-- output sum C Programming Language10
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 Language11
i ← 0, sum ← 0 false i < 5 int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(“%d”,&x); sum = sum + x; } true input x sum←sum+ x i++ printf(“%d”,sum); output sum C Programming Language12
for statement Example: for ( num = 1; num <= 3; num++ ) printf(“%d\t”, num); ??? num printf(“have come to exit\n”); 1 _ _ C Programming Language13
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); for statement 1 num printf(“have come to exit\n”); _ C Programming Language14
for statement 1 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num printf(“have come to exit\n”); _ C Programming Language15
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); for statement 1 num printf(“have come to exit\n”); 1 _ C Programming Language16
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); for statement 2 num printf(“have come to exit\n”); 1 _ C Programming Language17
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); for statement 2 num printf(“have come to exit\n”); 1 _ C Programming Language18
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); for statement 2 num printf(“have come to exit\n”); 1 2 _ C Programming Language19
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); for statement 3 num printf(“have come to exit\n”); 1 2 _ C Programming Language20
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); for statement 3 num printf(“have come to exit\n”); 1 2 _ C Programming Language21
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); for statement 3 num printf(“have come to exit\n”); 1 2 3 _ C Programming Language22
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); for statement 4 num printf(“have come to exit\n”); 1 2 3 _ C Programming Language23
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); for statement 4 num 1 2 3 _ C Programming Language24
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); for statement 4 num 1 2 3 have come to exit_ C Programming Language25
Loop Structure Condition is tested first Condition is tested later 4.3 Loop control with condition test C Programming Language26
Step a condition Step x Step y Step n Testing Condition First false true C Programming Language27
Testing Condition First Step a condition Step x Step y Step n C Programming Language28
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 Language29
Testing condition later Step a Step x Step y true condition false Step n C Programming Language30
Testing condition later Step a Step x Step y true condition false Step n C Programming Language31
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 Language32
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 Language33
Condition-ControlledLoop Assume m=57 Assume m=110 Assume m=5 57 5 110 m input m “WRONG INPUT” true m<0 || m>100 5 < 0 || 5 >100 110 < 0 || 110 >100 57 < 0 || 57 >100 false true Condition-controlled loop with its condition being tested at the end 5 > 49 m>49 57 > 49 “PASS” false “FAIL” PASS FAIL WRONG INPUT C Programming Language34
input m false m<0 || m>100 true “WRONG INPUT” input m Condition-controlled loop with its condition being tested first true m>49 “PASS” false “FAIL” C Programming Language35
int marks; scanf(“%d”,&marks); while (marks<0) | | (marks>100) { printf(“WRONG INPUT”); scanf(“%d”,&marks); } if (marks>49) { printf(“PASS”); else printf(“FAIL”); } Double Selection C Programming Language36
do-while statement Example : printf(“Input start and end value : “); scanf(“%d %d”, &start, &end); do { printf(“%c (%d)\n“, start, start); start++; } while (start <= end) ; 65 67 ??? 68 66 ??? 67 start end 67 <= 67 66 <= 67 68 <= 67 _ Input start and end value : 65 67_ Input start and end value : 65 67 A (65) B (66) _ Input start and end value : _ Input start and end value : 65 67 A (65) B (66) C (67) _ Input start and end value : 65 67 A (65) _ C Programming Language37
Can you identify the input and output??? 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 C Programming Language38
Sentinel Value • Input Example: 30 16 42 -9 • Output Example: Sum = 88 Average = 29.33 C Programming Language39
Now… What have you understand? C Programming Language40
What will happen if this statement is deleted??? ? Try to understand sum←0 input x What happened if these 2 statements exchange places false x>0 true input x sum←sum+x ? input x sum←sum+x display sum C Programming Language41
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 Language42
Sentinel-controlled loop sum=0 input x false x!=999 sum←sum+x input x output sum #include <stdio.h> 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 Language43
? x sum ? int sum, x; sum = 0; scanf(“%d”, &x); while (x != 999) { sum = sum + x; scanf(“%d”, &x); } printf(“\nThe sum : %d\n”, sum); 999 1 23 3 1 != 999 23 != 999 3 != 999 999 != 999 1+3 0 0+1 27 4 4+23 1 1 3 23 999 1 3 23 1 _ 1 3 The sum : 27 C Programming Language44
Now… Let us have a summary! • 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 Language45