Download presentation
Presentation is loading. Please wait.
Published byByron Wardle Modified over 9 years ago
1
Control Structure: Loop (Part 1) Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure 1
2
TK1913-C Programming2 TK1913-C Programming 2 Loop Structure Condition is tested firstCondition is tested later
3
TK1913-C Programming3 TK1913-C Programming 3 false true Testing Condition First condition Step x Step y Step a Step n
4
TK1913-C Programming4 TK1913-C Programming 4 Testing Condition First condition Step x Step y Step a Step n
5
TK1913-C Programming5 TK1913-C Programming 5 Step a condition Step n Step x false true Step y Testing condition later
6
TK1913-C Programming6 TK1913-C Programming 6 Step a condition Step n Step x false true Step y Testing condition later
7
TK1913-C Programming7 TK1913-C Programming 7 How Loops are Controlled Sentinel Controlled Counter Controlled 1, 2, 3, 4, … …, 4, 3, 2, 1 Condition Controlled
8
TK1913-C Programming8 TK1913-C Programming 8 Counter Controlled Loop counter ← initialValue test counter value Step n Step x false true Update counter
9
TK1913-C Programming9 TK1913-C Programming 9 Counter Controlled Loop counter = initialValue test counter value Step n Step x false true Update counter counter ← initialValue
10
TK1913-C Programming10 TK1913-C Programming 10 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
11
TK1913-C Programming11 TK1913-C Programming 11 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
12
TK1913-C Programming12 TK1913-C Programming 12 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
13
TK1913-C Programming13 TK1913-C Programming 13 Decreasing Counter-Controlled Loop counter ← 5, sum ← 0 counter > 0 sum←sum+ x false true counter-- output sum input x
14
TK1913-C Programming14 TK1913-C Programming 14 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
15
TK1913-C Programming15 TK1913-C Programming 15 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
16
TK1913-C Programming16 TK1913-C Programming 16 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
17
TK1913-C Programming17 TK1913-C Programming 17 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
18
TK1913-C Programming18 TK1913-C Programming 18 Input Example: 30 16 42 -9 Output Example: Sum = 88 Average = 29.33 Sentinel Value
19
TK1913-C Programming19 TK1913-C Programming 19 Now… What have you understand?
20
TK1913-C Programming20 TK1913-C Programming 20 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 ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.