Top Banner
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
41

8.2 Looping 1 Feb 2017

Feb 07, 2017

Download

Education

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 8.2 Looping 1 Feb 2017

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

Page 2: 8.2 Looping 1 Feb 2017

LOOPING CONTROL STRUCTURE

2

Page 3: 8.2 Looping 1 Feb 2017

The essentials of Looping

3

Page 4: 8.2 Looping 1 Feb 2017

The essentials of Looping

4

Page 5: 8.2 Looping 1 Feb 2017

The essentials of Looping

5

Page 6: 8.2 Looping 1 Feb 2017

The essentials of Looping

6

Page 7: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

7

Page 8: 8.2 Looping 1 Feb 2017

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

Page 9: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

9

Page 10: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

10

Page 11: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

bil=1

while (bil≤4)

bil=bil+1

False

True

Print “hello world”

11

end

Start

Page 12: 8.2 Looping 1 Feb 2017

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

Page 13: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

13

Page 14: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

14

Page 15: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

bil=4

while (bil≥1)

bil=bil-1

False

True

Print “hello world”

15

end

Start

Page 16: 8.2 Looping 1 Feb 2017

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

Page 17: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

17

Page 18: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

18

Page 19: 8.2 Looping 1 Feb 2017

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

Page 20: 8.2 Looping 1 Feb 2017

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

Page 21: 8.2 Looping 1 Feb 2017

Applying algorithm in looping structure

21

Page 22: 8.2 Looping 1 Feb 2017

22

End if

Page 23: 8.2 Looping 1 Feb 2017

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

Page 24: 8.2 Looping 1 Feb 2017

Applying algorithm in looping structure

24

Page 25: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

25

Page 26: 8.2 Looping 1 Feb 2017

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

Page 27: 8.2 Looping 1 Feb 2017

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

Page 28: 8.2 Looping 1 Feb 2017

Sentinel-controlled loop

28

Page 29: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

29

Page 30: 8.2 Looping 1 Feb 2017

Applying Algorithm in Looping construct

Answer = Y

while (answer ≠ N)

False

True

Print “I Love KMPK”

30

end

Start

Input answer

Page 31: 8.2 Looping 1 Feb 2017

Accumulating in problem solving

2 1 2 1

31

Page 32: 8.2 Looping 1 Feb 2017

Accumulating in problem solving

32

Page 33: 8.2 Looping 1 Feb 2017

Accumulating in problem solving

33

Page 34: 8.2 Looping 1 Feb 2017

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

Page 35: 8.2 Looping 1 Feb 2017

Accumulating in problem solving

35

Page 36: 8.2 Looping 1 Feb 2017

36

Page 37: 8.2 Looping 1 Feb 2017

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

Page 38: 8.2 Looping 1 Feb 2017

Summary looping- basic loop- sentinel-controlled loop- accumulating loop

38

Page 39: 8.2 Looping 1 Feb 2017

39

bil=1

while (bil≤4)

bil=bil+1

False

TruePrint

“hello world”

end

Start

Basic loop

Page 40: 8.2 Looping 1 Feb 2017

40

end

Start

Sentinel-controlled loop

Answer = Y

while (answer ≠ N)

False

True

Print “I Love KMPK”

Input answer

Page 41: 8.2 Looping 1 Feb 2017

41

end

Start

Accumulating loop

bil=1, sum = 0

While (bil≤50)

bil=bil+1

False

True

Input number

sum=sum + number

Print sum