Science 7 - LAND and SEA BREEZE and its Characteristics
8.2 Looping 1 Feb 2017
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
8. 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
11. Applying Algorithm in Looping construct
bil=1
while (bil≤4)
bil=bil+1
False
True
Print “hello
world”
11
end
Start
12. Applying Algorithm in Looping construct
Bil Condition
Bil≤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
15. Applying Algorithm in Looping construct
bil=4
while (bil≥1)
bil=bil-1
False
True
Print “hello
world”
15
end
Start
16. Applying Algorithm in Looping construct
Bil Condition
Bil≥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
19. 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
20. Applying Algorithm in Looping construct
Bil Condition
Bil≤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
26. 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
27. 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
34. 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
37. 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