Download presentation
Presentation is loading. Please wait.
1
LOOP (Part 2) for while do-while 1
2
TK1913-C Programming2 TK1913-C Programming 2 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; }
3
TK1913-C Programming3 TK1913-C Programming 3 Example Write a program which does the following: Reads 5 integers and displays the sum of all integers Input example: 36412 Output example: 16
4
TK1913-C Programming4 TK1913-C Programming 4 Recall the flowchart counter ← 1, sum ← 0 counter < 6 sum←sum+ x false true counter++ output sum input x
5
TK1913-C Programming5 TK1913-C Programming 5 Note the initial value of i and condition i ← 1, sum ← 0 i < 6 sum←sum+ x false true i++ output sum input x How many times does the loop get executed?
6
TK1913-C Programming6 TK1913-C Programming 6 i ← 0, sum ← 0 i < 6 sum←sum+ x false true i++ output sum input x How many times does the loop get executed?
7
TK1913-C Programming7 TK1913-C Programming 7 i ← 0, sum ← 0 i < 5 sum←sum+ x false true i++ output sum input x How many times does the loop get executed?
8
TK1913-C Programming8 TK1913-C Programming 8 The C statements: int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(“%d”,&x); sum = sum + x; } printf(“%d”,sum);
9
TK1913-C Programming9 TK1913-C Programming 9 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);
10
TK1913-C Programming10 TK1913-C Programming 10 for statement for statement Example: for ( num = 1; num <= 3; num++ ) printf(“%d\t”, num); num ??? _ 1_1_
11
TK1913-C Programming11 TK1913-C Programming 11 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 1 _ for statement for statement
12
TK1913-C Programming12 TK1913-C Programming 12 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 1 _ for statement for statement
13
TK1913-C Programming13 TK1913-C Programming 13 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 1 1_1_ for statement for statement
14
TK1913-C Programming14 TK1913-C Programming 14 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 2 1_1_ for statement for statement
15
TK1913-C Programming15 TK1913-C Programming 15 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 2 1_1_ for statement for statement
16
TK1913-C Programming16 TK1913-C Programming 16 for statement for statement Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); bil 2 12_12_
17
TK1913-C Programming17 TK1913-C Programming 17 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 3 12_12_ for statement for statement
18
TK1913-C Programming18 TK1913-C Programming 18 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 3 12_12_ for statement for statement
19
TK1913-C Programming19 TK1913-C Programming 19 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 3 123_123_ for statement for statement
20
TK1913-C Programming20 TK1913-C Programming 20 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 4 123_123_ for statement for statement
21
TK1913-C Programming21 TK1913-C Programming 21 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 4 123_123_ for statement for statement
22
TK1913-C Programming22 TK1913-C Programming 22 for while do-while
23
TK1913-C Programming23 TK1913-C Programming 23 Loop: while Loop: while Condition is tested first Loop is controlled by condition or a counter Syntax while (condition) statement; Or while (condition) { statement; }
24
TK1913-C Programming24 TK1913-C Programming 24 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
25
TK1913-C Programming25 TK1913-C Programming 25 false true input m m 100 m>49 “PASS” “FAIL” true false “WRONG INPUT” input m Exercise: Convert this flowchart to a C program
26
TK1913-C Programming26 TK1913-C Programming 26 int marks; scanf(“%d”,&marks); while (marks 100) { printf(“WRONG INPUT”); scanf(“%d”,&marks); } if (marks>49) { printf(“PASS”); else printf(“FAIL”); } Double Selection
27
TK1913-C Programming27 TK1913-C Programming 27 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
28
TK1913-C Programming28 TK1913-C Programming 28 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); }
29
TK1913-C Programming29 TK1913-C Programming 29 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
30
TK1913-C Programming30 TK1913-C Programming 30 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; } while (condition); statement;
31
TK1913-C Programming31 TK1913-C Programming 31 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
32
TK1913-C Programming32 TK1913-C Programming 32 continue statement Example: for ( i = 0; i <= 5; i++ ) { if ( i % 2 ) continue; else printf(“%d is an even number. ”, i); printf(“Print iff even ! \n”); } ii <= 5i % 2 _ 00 <= 9 true01 <= 5 true12 <= 5 true3 <= 5 true4 <= 5 true55 <= 5 true166 <= 5 false2341010 0 is an even number._0 is an even number. Print iff even ! _ 0 is an even number. Print iff even ! 2 is an even number._ 0 is an even number. Print iff even ! 2 is an even number. Print iff even ! _ 0 is an even number. Print iff even ! 2 is an even number. Print iff even ! 4 is an even number._ 0 is an even number. Print iff even ! 2 is an even number. Print iff even ! 4 is an even number. Print iff even ! _
33
TK1913-C Programming33 TK1913-C Programming 33 Now what can you conclude about continue?
34
TK1913-C Programming34 TK1913-C Programming 34 break statement break statement 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”); value ??? ii <= 7i == value Input a value between 1-7: _Input a value between 1-7: 4 11 <= 7 true1 == 4 false 4 Input a value between 1-7: 4 1 22 <= 7 true2 == 4 false Input a value between 1-7: 4 1 2 33 <= 7 true3 == 4 false Input a value between 1-7: 4 1 2 3 44 <= 7 true4 == 4 true Input a value between 1-7: 5 1 2 3YeePee! I’m out of the loop!
35
TK1913-C Programming35 TK1913-C Programming 35 Now what can you conclude about break?
36
TK1913-C Programming36 TK1913-C Programming 36 End of 1 st half of the semester Yes !! That’s all? What’s next??? 1 week HOLIDAY on the way …
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.