APPROACH IN PROBLEM SOLVING - Control Structure • At the end of lesson, student should able to : - explain the purpose of looping control structure - apply looping control strcuture in problem soving - LOOPING / REPETITION 1
APPROACH IN PROBLEM SOLVING- Control Structure
• At the end of lesson, student should able to :- explain the purpose of looping control structure- apply looping control strcuture in problem soving
- LOOPING / REPETITION
1
LOOPING CONTROL STRUCTURE
2
The essentials of Looping
3
The essentials of Looping
4
The essentials of Looping
5
The essentials of Looping
6
Applying Algorithm in Looping construct
7
Applying Algorithm in Looping construct
Initial value
while (condition)
statement(s) to be executed if
condition is TRUE
Updating value
False
True
8
end
Start
Applying Algorithm in Looping construct
9
Applying Algorithm in Looping construct
10
Applying Algorithm in Looping construct
bil=1
while (bil≤4)
bil=bil+1
False
True
Print “hello world”
11
end
Start
Applying Algorithm in Looping construct
Bil ConditionBil≤4
Output Updating, bil=bil+1
1 T Hello world 2
2 T Hello world 3
3 T Hello world 4
4 T Hello world 5
5 F
12
Applying Algorithm in Looping construct
13
Applying Algorithm in Looping construct
14
Applying Algorithm in Looping construct
bil=4
while (bil≥1)
bil=bil-1
False
True
Print “hello world”
15
end
Start
Applying Algorithm in Looping construct
Bil ConditionBil≥1
Output Updating, bil=bil-1
4 T Hello world 3
3 T Hello world 2
2 T Hello world 1
1 T Hello world 0
0 F
16
Applying Algorithm in Looping construct
17
Applying Algorithm in Looping construct
18
Applying Algorithm in Looping construct
bil=1
while (bil≤5)
bil=bil+1
False
True
Input width, height
area = width x height
Print area
19
end
Start
Applying Algorithm in Looping construct
Bil ConditionBil≤4
Input width, height
area=width x height
Output; area
Updating, bil=bil+1
1 T 2
2 T 3
3 T 4
4 T 5
5 T 6
6 F
20
Applying algorithm in looping structure
21
22
End if
bil=1
while (bil≤20)
bil=bil+1
False
True
Input mark
Print “status Pass”
if (mark≥50)
Print “status Fail”
True
False
23
end
Start
Applying algorithm in looping structure
24
Applying Algorithm in Looping construct
25
Applying Algorithm in Looping construct
bil=1
while (bil≤x)
bil=bil+1
False
True
Input radius
area = 22/7 x radius x radius
Print area
Input x
26
end
Start
Sentinel-controlled loop
✓ A sentinel variable is initialized to a specific value. The while loop continues until, through some action inside the loop, the sentinel variable is set to a predefined termination value.
✓ In the following example the user is asked to type characters at the keyboard, which are then appear on the screen, then press the Enter key when done.
✓ Pressing the Enter key sets the char variable answer to the ‘N’ character (defined by the special character ‘N').
27
Sentinel-controlled loop
28
Applying Algorithm in Looping construct
29
Applying Algorithm in Looping construct
Answer = Y
while (answer ≠ N)
False
True
Print “I Love KMPK”
30
end
Start
Input answer
Accumulating in problem solving
2 1 2 1
31
Accumulating in problem solving
32
Accumulating in problem solving
33
Accumulating in problem solving
bil=1, sum = 0
While (bil≤50)
bil=bil+1
False
True
Input number
sum=sum + number
Print sum
* sum printed when condition is FALSE. Its because, when the condition is FALSE, that means all the 50 numbers already calculated by program
34
end
Start
Accumulating in problem solving
35
36
Accumulating in problem solving
bil=1, sum = 0
while (bil≤50)
bil=bil+1
False
True
Input number
sum=sum + number
Print sum , average
* Average printed when condition is FALSE. Its because, average can calculated when all the number were input
average=sum/50
37
end
Start
Summary looping- basic loop- sentinel-controlled loop- accumulating loop
38
39
bil=1
while (bil≤4)
bil=bil+1
False
TruePrint
“hello world”
end
Start
Basic loop
40
end
Start
Sentinel-controlled loop
Answer = Y
while (answer ≠ N)
False
True
Print “I Love KMPK”
Input answer
41
end
Start
Accumulating loop
bil=1, sum = 0
While (bil≤50)
bil=bil+1
False
True
Input number
sum=sum + number
Print sum