210 likes | 297 Views
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. Condition is tested first. Condition is tested later. Loop Structure. Testing Condition First. Step a. false.
E N D
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
Condition is tested first Condition is tested later Loop Structure TK1913-C Programming2
Testing Condition First Step a false condition true Step x Step y Step n TK1913-C Programming3
Testing Condition First Step a condition Step x Step y Step n TK1913-C Programming4
Testing condition later Step a Step x Step y true condition false Step n TK1913-C Programming5
Testing condition later Step a Step x Step y true condition false Step n TK1913-C Programming6
Sentinel Controlled Condition Controlled • Counter Controlled • 1, 2, 3, 4, … • …, 4, 3, 2, 1 How Loops are Controlled TK1913-C Programming7
Counter Controlled Loop counter ← initialValue false test counter value true Step x Update counter Step n TK1913-C Programming8
Counter Controlled Loop counter = initialValue counter ← initialValue false test counter value true Step x Update counter Step n TK1913-C Programming9
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 TK1913-C Programming10
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 TK1913-C Programming11
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 TK1913-C Programming12
Decreasing Counter-Controlled Loop counter ← 5, sum ← 0 false counter > 0 true input x sum←sum+ x counter-- output sum TK1913-C Programming13
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 Programming14
while ( ulang=y); • { • } TK1913-C Programming15
Assume m=57 Assume m=110 Assume m=5 Condition-ControlledLoop 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 TK1913-C Programming16
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” TK1913-C Programming17
Can you identify the input and output??? 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 TK1913-C Programming18
Sentinel Value • Input Example: 30 16 42 -9 • Output Example: Sum = 88 Average = 29.33 TK1913-C Programming19
Now… What have you understand? TK1913-C Programming20
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 TK1913-C Programming21