Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.

Similar presentations


Presentation on theme: "Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop."— Presentation transcript:

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

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. SequenceSelectionRepetition or loop

4 C Programming Language4 C Programming Language 4 How Loops are Controlled? Sentinel Controlled Counter Controlled 1, 2, 3, 4, … …, 4, 3, 2, 1 Condition Controlled

5 C Programming Language5 C Programming Language 5 4.2 Counter Controlled Loop counter ← initial Value test counter value Step n Step x false true Update counter

6 C Programming Language6 C Programming Language 6 Counter Controlled Loop counter = initialValue test counter value Step n Step x false true Update counter counter ← initial Value

7 C Programming Language7 C Programming Language 7 Example: Can you identify the input and output??? 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 C Programming Language8 C Programming Language 8 Input n1 Input n2 Input n3 input n4 input n5 output sum sum ← n1+n2+n3+n4+n5 start 2 n1 Assume input example: 2 3 4 5 6 3 n2 4 n3 5 n4 6 n5 20 sum end This flowchart does not use loop, hence we need to use 6 different variables

9 C Programming Language9 C Programming Language 9 Counter- Controlled Loop counter ← 1, sum ← 0 counter < 6 sum ← sum + n false true counter++ output sum input n 1 counter sum 0 1 < 6true 2 n 0 + 2 2 2 2 < 6true 3 2 + 3 5 3 3 < 6true 4 5 + 4 9 4 4 < 6true 5 9 + 5 14 5 5 < 6true 6 14 + 6 20 6 6 < 6false Assume input example: 2 3 4 5 6 This loop is counter-controlled The counter Increases by 1 Uses only 3 variables

10 C Programming Language10 C Programming Language 10 Decreasing Counter-Controlled Loop counter ← 5, sum ← 0 counter > 0 sum←sum+ x false true counter-- output sum input x

11 C Programming Language11 C Programming Language 11 Loop : for 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; }

12 C Programming Language12 C Programming Language 12 i ← 0, sum ← 0 i < 5 sum←sum+ x false true i++ output sum input x int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(“%d”,&x); sum = sum + x; } printf(“%d”,sum);

13 C Programming Language13 C Programming Language 13 for statement for statement Example: for ( num = 1; num <= 3; num++ ) printf(“%d\t”, num); num ??? _ 1_1_ printf(“have come to exit\n”);

14 C Programming Language14 C Programming Language 14 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 1 _ for statement for statement printf(“have come to exit\n”);

15 C Programming Language15 C Programming Language 15 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 1 _ for statement for statement printf(“have come to exit\n”);

16 C Programming Language16 C Programming Language 16 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 1 1_1_ for statement for statement printf(“have come to exit\n”);

17 C Programming Language17 C Programming Language 17 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 2 1_1_ for statement for statement printf(“have come to exit\n”);

18 C Programming Language18 C Programming Language 18 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 2 1_1_ for statement for statement printf(“have come to exit\n”);

19 C Programming Language19 C Programming Language 19 for statement for statement Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 2 12_12_ printf(“have come to exit\n”);

20 C Programming Language20 C Programming Language 20 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 3 12_12_ for statement for statement printf(“have come to exit\n”);

21 C Programming Language21 C Programming Language 21 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 3 12_12_ for statement for statement printf(“have come to exit\n”);

22 C Programming Language22 C Programming Language 22 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 3 123_123_ for statement for statement printf(“have come to exit\n”);

23 C Programming Language23 C Programming Language 23 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 4 123_123_ for statement for statement printf(“have come to exit\n”);

24 C Programming Language24 C Programming Language 24 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 4 123_123_ for statement for statement

25 C Programming Language25 C Programming Language 25 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 4 123have come to exit_ for statement for statement

26 C Programming Language26 C Programming Language 26 Loop Structure Condition is tested firstCondition is tested later 4.3 Loop control with condition test

27 C Programming Language27 C Programming Language 27 false true Testing Condition First condition Step x Step y Step a Step n

28 C Programming Language28 C Programming Language 28 Testing Condition First condition Step x Step y Step a Step n

29 C Programming Language29 C Programming Language 29 Loop: while Loop: while  Condition is tested first  Loop is controlled by condition or a counter  Syntax while (condition) statement; Or while (condition) { statement; }

30 C Programming Language30 C Programming Language 30 Step a condition Step n Step x false true Step y Testing condition later

31 C Programming Language31 C Programming Language 31 Step a condition Step n Step x false true Step y Testing condition later

32 C Programming Language32 C Programming Language 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; } while (condition); statement;

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

34 C Programming Language34 C Programming Language 34 Condition-Controlled Loop false true input m m 100 m>49 “PASS” “FAIL” true false “WRONG INPUT” Assume m=110 m 110 110 100 WRONG INPUT Assume m=5 5 5 100 5 > 49 FAIL Assume m=57 57 57 100 57 > 49 PASS Condition-controlled loop with its condition being tested at the end

35 C Programming Language35 C Programming Language 35 false true input m m 100 m>49 “PASS” “FAIL” true false “WRONG INPUT” input m Condition-controlled loop with its condition being tested first

36 C Programming Language36 C Programming Language 36 int marks; scanf(“%d”,&marks); while (marks 100) { printf(“WRONG INPUT”); scanf(“%d”,&marks); } if (marks>49) { printf(“PASS”); else printf(“FAIL”); } Double Selection

37 C Programming Language37 C Programming Language 37 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) ; _ ??? start ??? end Input start and end value : _Input start and end value : 65 67_ 6567 Input start and end value : 65 67 A (65) _ 66 66 <= 67 Input start and end value : 65 67 A (65) B (66) _ 67 <= 67 67 Input start and end value : 65 67 A (65) B (66) C (67) _ 68 68 <= 67

38 C Programming Language38 C Programming Language 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 Can you identify the input and output??? Input: A set of integers ending with a negative integer or a zero Output: Summation and Average of these integers

39 C Programming Language39 C Programming Language 39 Input Example: 30 16 42 -9 Output Example: Sum = 88 Average = 29.33 Sentinel Value

40 C Programming Language40 C Programming Language 40 Now… What have you understand?

41 C Programming Language41 C Programming Language 41 x>0 sum←sum+x false true input x sum←0 input x display sum Try to understand What will happen if this statement is deleted??? ? What happened if these 2 statements exchange places sum←sum+x input x ?

42 C Programming Language42 C Programming Language 42 Exercise Given a set of integers with the last one being 999 Display the summation of all the integers. Input example: 1323999 Output example: Sum = 27 Draw the flowchart for this problem

43 C Programming Language43 C Programming Language 43 Sentinel-controlled loop true x!=999 sum←sum+x false input x sum=0 input x output sum #include 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); }

44 C Programming Language44 C Programming Language 44 int sum, x; sum = 0; scanf(“%d”, &x); while (x != 999) { sum = sum + x; scanf(“%d”, &x); } printf(“\nThe sum : %d\n”, sum); ? ? x sum 0 1 _1 1 != 999 0+11 1313 3 3 != 999 1+34 1323 23 23 != 999 4+2327 1323999 999 999 != 999 The sum : 27

45 C Programming Language45 C Programming Language 45 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!


Download ppt "Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop."

Similar presentations


Ads by Google