Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure 1
Loop Structure Condition is tested first Condition is tested later TK1913-C Programming 2
Testing Condition First Step a condition false true Step x Step y Step n TK1913-C Programming 3
Testing Condition First Step a condition Step x Step y Step n TK1913-C Programming 4
Testing condition later Step a Step x Step y true condition false Step n TK1913-C Programming 5
Testing condition later Step a Step x Step y true condition false Step n TK1913-C Programming 6
How Loops are Controlled Condition Controlled Counter Controlled 1, 2, 3, 4,, 4, 3, 2, 1 Sentinel Controlled TK1913-C Programming 7
Counter Controlled Loop counter initialvalue true test counter value Step x false Update counter Step n TK1913-C Programming 8
Counter Controlled Loop counter = initialvalue true test counter value Step x false Update counter Step n TK1913-C Programming 9
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
Assume input example: 2 3 4 5 6 start Input n1 n1 2 This flowchart does not use loop, hence we need to use 6 different variables Input n2 Input n3 input n4 n2 n3 n4 3 4 5 input n5 n5 6 sum n1+n2+n3+n4+n5 sum 20 output sum end TK1913-C Programming 11
Counter- Assume input Controlled example: 2 3 4 5 6 Loop counter 1, sum 0 false counter < 6 true counter 12 34 56 sum 14 20 02 59 14 02 59 + 23 45 6 61 23 54 < < 6 6 false true input n n 23 45 6 sum sum + n counter++ output sum The Uses This counter loop only is counter-controlled Increases 3 variables by 1 TK1913-C Programming 12
Decreasing Counter-Controlled Loop counter 5, sum 0 true counter > 0 input x sum sum+ x false counter-- output sum TK1913-C Programming 13
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 TK1913-C Programming 14
Condition-Controlled Loop Assume m=110 m=57 m 110 57 5 input m WRONG INPUT 110 57 < < 0 < 0 0 5 57 >100 110 >100 >100 true m<0 m>100 false 5 > Condition-controlled 57 49 > 49 loop with its condition being tested at the end false m>49 PASS FAIL true WRONG FAIL PASS INPUT TK1913-C Programming 15
input m m<0 m>100 true WRONG INPUT false input m Condition-controlled loop with its condition being tested first false m>49 PASS FAIL true TK1913-C Programming 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 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 TK1913-C Programming 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 sum 0 input x x>0 true sum sum+x input x false What will happen? if this statement is deleted??? What happened if these 2 statements exchange places sum sum+x input x display sum? TK1913-C Programming 20
for while do-while TK1913-C Programming 21
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; } TK1913-C Programming 22
Example Write a program which does the following: Reads 5 integers and displays the sum of all integers Input example: 3 6 4 1 2 Output example: 16 TK1913-C Programming 23
Recall the flowchart counter 1, sum 0 true counter < 6 input x sum sum+ x false counter++ output sum TK1913-C Programming 24
Note the initial value of i and condition How many times does the loop get executed? i 1, sum 0 true i < 6 input x sum sum+ x i++ false output sum TK1913-C Programming 25
i 0, sum 0 How many times does the loop get executed? true i < 6 input x sum sum+ x i++ output sum false TK1913-C Programming 26
i 0, sum 0 How many times does the loop get executed? true i < 5 input x sum sum+ x i++ output sum false TK1913-C Programming 27
The C statements: int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf( %d,&x); sum = sum + x; } printf( %d,sum); TK1913-C Programming 28
i 0, sum 0 true i < 5 input x sum sum+ x i++ output sum false int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf( %d,&x); sum = sum + x; } printf( %d,sum); TK1913-C Programming 29
for statement Example: for ( num = 1; num <= 3; num++ ) printf( %d\t, num);??? num 1 TK1913-C Programming 30
for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 1 num _ TK1913-C Programming 31
for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 1 num _ TK1913-C Programming 32
for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 1 num 1 _ TK1913-C Programming 33
for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 2 num 1 _ TK1913-C Programming 34
for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 2 num 1 _ TK1913-C Programming 35
for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 2 bil 1 2 _ TK1913-C Programming 36
for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 3 num 1 2 _ TK1913-C Programming 37
for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 3 num 1 2 _ TK1913-C Programming 38
for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 3 num 1 2 3 _ TK1913-C Programming 39
for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 4 num 1 2 3 _ TK1913-C Programming 40
for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 4 num 1 2 3 _ TK1913-C Programming 41
for while do-while TK1913-C Programming 42
Loop: while Condition is tested first Loop is controlled by condition or a counter Syntax while (condition) statement; Or while (condition) { statement; statement; } TK1913-C Programming 43
Recall this example: 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 TK1913-C Programming 44
input m m<0 m>100 true WRONG INPUT false Exercise: Convert this flowchart to a C program input m false m>49 PASS FAIL true TK1913-C Programming 45
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 TK1913-C Programming 46
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 Draw the flowchart for this problem Output example: Sum = 27 TK1913-C Programming 47
Sentinel-controlled loop sum=0 input x x!=999 sum sum+x input x output sum false true #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); } TK1913-C Programming 48
int sum, x; sum = 0; scanf( %d, &x); while (x!= 999) { sum = sum + x; scanf( %d, &x); 13 23 999!=!= 999!= 999 } printf( \nthe sum : %d\n, sum); x 999 23 13? sum 4+23 0+1 1+3 27 01 4? _ 1 3 23 999 The sum : 27 TK1913-C Programming 49
Do-while Loop Statements in the loop are executed first (at least onc, and condition is tested last Loop is controlled by a condition or counter Syntax do { statement; statement; } while (condition); statement; TK1913-C Programming 50
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) ; 66 67 68 <= 67 _ Input start and end value : _ 65 67_ A (65) _ B (66) _ C (67) _??? 65 66 67 68??? 67 start end TK1913-C Programming 51
_ 0 is an even number._ Print iff even! continue statement _ 2 is an even number._ Print iff even! _ 4 is an even number._ Print iff even! Example: _ for ( i = 0; i <= 5; i++ ) { if ( i % 2 ) continue; else printf( %d is an even number., i); printf( Print iff even! \n ); } i i <= 5 i % 2 01 56 23 4 601 23 4 <= 5 <= 59 false 5 true 10 TK1913-C Programming 52
Now what can you conclude about continue? TK1913-C Programming 53
break statement Input a value between 1-7: _ 45 1 2 3YeePee! I m out of the loop! printf( Input a value between 1 7: ); scanf( %d,&value); for (i = 1; i <= 7; i++) { if ( i = = value ) break; printf( \n%d,i); } printf( YeePee! I m out of the loop!\n );??? 4 value i i <= 7 i == value 12 34 42 31 <= <= 7 7 true 23 14 == 4 false true TK1913-C Programming 54
Now what can you conclude about break? TK1913-C Programming 55
End of 1 st half of the semester Yes!! That s all? What s next??? Procedure & Function TK1913-C Programming 56