pendalaman materi conditional dan repetition ( pengulangan )

46
Pendalaman Materi Conditional dan Repetition (Pengulangan)

Upload: semah

Post on 23-Feb-2016

40 views

Category:

Documents


0 download

DESCRIPTION

Pendalaman Materi Conditional dan Repetition ( Pengulangan ). IF THEN Bersarang. Input (nilai) IF nilai > 85 AND nilai 60 AND nilai 45 AND nilai

TRANSCRIPT

Page 1: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

Pendalaman Materi Conditional dan Repetition (Pengulangan)

Page 2: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

IF THEN BersarangInput (nilai)

IF nilai > 85 AND nilai <= 100 THENOutput (‘Nilai mutu = A’)ELSEIF nilai > 60 AND nilai <= 85 THENOutput (‘Nilai mutu = B’)ELSEIF nilai > 45 AND nilai <= 60 THENOutput (‘Nilai mutu = C’)ELSEIF nilai > 30 AND nilai <= 45 THENOutput (‘Nilai mutu = D’)ELSEOutput (‘Nilai mutu = E’)

5. The value variable of nilai = 60, the result is ‘Nilai mutu = B’.6. The value variable of nilai = 101, the result is ‘Nilai mutu = E’. 7. The value variable of nilai = -5, the result is ‘Error’.

Page 3: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

Write the algorithm to calculate cost of electricitywith following constraints :

(KWHs) Rate 0 - 500 2000501 - 1000 2000 + 0.03 * per KWH under 500 over 1001 3500 + 0.02 * per KWH under 1000

• Enter the values of previous month meters, current month meter, and then displays the total cost which would exist.

Page 4: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

4

Loops notation

1. While-Do2. Repeat-Until,and 3. For-to-Do

Page 5: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

5

WHILE-DO-ENDWHILE

• it allows a {group of} program statement(s) to be executed a number of times.

• The program statement(s) are executed when the condition evaluates as true.

• Somewhere inside the loop the value of the variable which is controlling the loop (ie, being tested in the condition) must change so that the loop can finally exit.

Page 6: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

6

The structure of the WHILE... DO... statement

... {initiation}WHILE (condition) DO

program statementprogram statement…counter statement

...• Counted loops are special types of pre-test loops. They are used when a known number of

repetitions will occur.• condition is the Boolean Expression, whether it be AND, OR, or NOT, is first carried out. If the result

is TRUE, the DO statement will then be performed. However, if the result is FALSE, the DO statement will not be executed

• Iteration will be controlled by Counter statement, wether it be increase or decrease• The value of counter will be changed at the end of loop.

Page 7: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

7

Flowchart WHILE... DO... START

END

condition = true?

action

{initiationi}

{changing counter}

yes

No

Page 8: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

8

Example 1

PseudocodeWHILE there is pressure on the mat Sound the bell

Flowchart

Pressureon mat?

Sound bell

T

F

Page 9: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

9

Example 2

PseudocodeOUTPUT('before loop starts') i 0 { Initialize counter variable }WHILE (i <= 5) DO { Loop with predicate in ()}

i i + 1 {Increment counter variable}OUTPUT('iteration number ',i)

OUTPUT('after loop ends')

Page 10: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

10

Execution • generates the output

Iteration no. i i <= 5 ? i i+1 Output

before loop starts1 0 True 1 iteration number

12 1 True 2 iteration number

23 2 True 3 iteration number

34 3 True 4 iteration number

45 4 True 5 iteration number

56 5 True 6 iteration number

67 6 False after loop ends

Here, the loop predicate is the relational test "i <= 5", which causes the loop to terminate after i is incremented to six.

Page 11: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

11

Example 3i 0 {initialize}WHILE (i < 5) DO {number of iteration}

Output (‘*’) {statement}i i + 1 {increment counter}

The result of program :*****

Page 12: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

12

Example 4• Program with enter the value of 3 student’s scores :

Pseudocode i 1WHILE i <= 3 DO

Output (‘Score student no-‘,i,’ is:’)Input (score)i i + 1

• Execution :– Score student no-1 is : __– Score student no-2 is : __– Score student no-3 is : __

Page 13: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

13

Example 5

• Average of 3 numbersnumber 0 {initialize}count 3 {counters}WHILE count > 0 DO

Input (x) {enter value of x}number number + x {sum of x variables}count count – 1 {decrement counter variable}

average number/ 3 {calculate the average}Output (average)

Page 14: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

14

REPEAT-UNTIL• Post-test loops tests the condition at the end of the loop.• The process in the loop is executed first whether it is true or false

and will keep repeating until the condition is met.• Post-test loops end when the condition is true but the loop always

do the loop at least once, even if the end condition is originally true.

• Post-test loops are also called unguarded loops because no check is made before the algorithm begins the loop structure.

Page 15: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

15

The structure of the REPEAT-UNTIL statement

... {initialize}REPEAT

program statementcounter variable

UNTIL (condition)...

Page 16: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

16

Flowchart REPEAT-UNTILMULAI

aksi

{inisialisasi}

END

condition = true?

START

Program statement {action}

{initialize}

Counter changes

No

yes

Page 17: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

17

Example 1

Pseudocode

REPEAT

Take out one item

UNTIL bag is empty Isbag

empty?

Flowchart

Take out item

T

F

Page 18: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

18

Example 2

PseudocodeOUTPUT('before loop starts') i 0 { Initialize counter variable }REPEAT

i i + 1 {Increment counter variable}OUTPUT('iteration number ',i)

UNTIL (i= 5) { termination}

OUTPUT('after loop ends')

Page 19: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

19

Execution • generates the output

Iteration no.

i i i+1 Output i = 5 ?

before loop starts1 0 1 iteration number

1False

2 1 2 iteration number 2

False

3 2 3 iteration number 3

False

4 3 4 iteration number 4

False

5 4 5 iteration number 5

True

6 5 6 after loop ends

Page 20: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

20

Example 3i 0 {initialize}REPEAT

Output (‘*’) {action statement}i i + 1 {increment counter}

UNTIL i=5 {end loop}The result of program :

*****

Page 21: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

21

Example 4• Program with enter the value of 3 student’s scores :

Pseudocode i 1REPEAT

Output (‘Score student no-‘,i,’ is:’)Input (score)i i + 1

UNTIL i = 3• Execution :

– Score student no-1 is : __– Score student no-2 is : __– Score student no-3 is : __

Page 22: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

22

Example 5

• Average of 3 numbersnumber 0 {initialize}count 3 {counters}REPEAT

Input (x) {input value of x}number number + x {sum of x variables}count count – 1 {decrement counter variable}

UNTIL count=0average number/ 3 {calculate the average}Output (average)

Page 23: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

23

FOR-TO-DO• The statement inside the for block is executed a number of times

depending on the control condition. • A loop index or loop counter is an integer variable that is used to

keep track of how many times a loop has executed .• A loop limit is a variable or constant that is integer-valued, which

determines the number of times a loop will execute, or a maximum value of the loop index to be reached at loop termination. .

• A loop increment is the step size for incrementing the loop counter .

Page 24: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

24

The structure of the FOR-TO-DO statement

FOR counter variable initial_value TO final_value program statement

Page 25: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

25

Flowchart FOR-TO-DO

No

yes

START

END

Counter >= initial_value AND

counter <= final_value ?

action

{initialize}

Page 26: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

26

Example 1

Pseudocode

FOR x1 TO 5 DO

output (x)

Flowchart

x >=1 AND x <= 5 ?

START

ENDoutput (x)

No

yes

Page 27: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

27

Example 2

PseudocodeOUTPUT('before loop starts') FOR i = 1 TO 5 DO

OUTPUT('iteration number ',i)

OUTPUT('after loop ends')

Page 28: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

28

Execution • generates the output

Iteration no.

i i >=1 AND

i<= 5 ?

Output

before loop starts1 1 True iteration number

12 2 True iteration number

23 3 True iteration number

34 4 True iteration number

45 5 True iteration number

56 6 False after loop ends

Page 29: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

29

Example 3

FOR i 1 TO 5 DO Output (‘*’) {action statement}

The result of program :*****

Page 30: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

30

Example 4• Program with enter the value of 3 student’s scores :

Pseudocode

FOR i 1 TO 3 DOOutput (‘Score student no-‘,i,’ is:’)Input (score)

• Execution :– Score student no-1 is : __– Score student no-2 is : __– Score student no-3 is : __

Page 31: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

31

Example 5

• Average of 3 numbersnumber 0 {initialize}FOR i 1 TO 3 DO

Input (x) {input value of x}number number + x {sum of x variables}

average number/ 3 {calculate the average}Output (average)

Page 32: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

32

Contoh Nested Loop (pengulangan bersarang)

FOR i 0 TO 4 DOFOR j 0 TO 2 DO

Output (‘*’) {aksi}

Program output :***************

Page 33: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

33

Contoh Nested Loop (pengulangan bersarang)

• Input values of 3 student’s score that have 2 different valuesFOR i1 TO 3 DO

Output (‘Student score no-‘,i,’ are ’)FOR j1 TO 2 DO

Output (‘Score ’,j,’ :’) Input (score)

• Running : Student score no- 1 areScore, 1 : _Score, 2 : _Student score no- 2 areScore, 1 : _Score, 2 : _Student score no- 3 areScore, 1 : _Score, 2 : _

Page 34: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

Soal 1What are values of the variable x after the execution of the followingpseudocode?

n 0 x 0While n < 15 Do If (n mod 3) = 0 Then

x x + n Output(x) n n +1

A. 1, 3, 6, 9, 12B. 0, 3, 6, 9,12C. 3, 18, 24D. 0, 3, 9,18, 30E. 3,6,9,12

Page 35: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

Soal 2What is the output of the following pseudocode?

FOR i 1 TO 2 DOIF i mod 2 = 0 THEN FOR j 1 TO 4 DO IF j mod 2 = 1 THEN Output (‘x’) ELSE Output (‘o’)ELSE Output (i)

A. ixoxo

B. xoxoi

C. ioxox

D. oxoxi

E. ii

Page 36: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

36

Soal 3

Consider the following statement– INPUT : 7– PROCESS : 1+2+3+4+5+6+7– OUTPUT : 28

• Create your pseudocode

Page 37: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

37

Soal 4

• Determine the program of the following output,

**********

Page 38: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

38

Soal 5

• Determine the program of the following output,

**********

Page 39: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

39

Soal 6• What is the output of the following code

{Declare}X,i : IntegerAlgorithm

For X 1 to 10IF (X mod 2= 0) THEN

Output(X) i i + 1

Output(i)

Page 40: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

40

Soal 7Describe the output of the following programfor i 1 to 3 do for j 5 to 6 do Output(i,j)

Page 41: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

41

Soal 8

• Write a program that it display the traditional song “Anak Ayam”. Enter the first anak ayam from keyboard.

Page 42: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

42

Soal 9

• Write a program that display ‘*’ on even line and displays ‘#’ on odd line. Enter number of line from keyboard

Page 43: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

43

Soal 10

• Write a program which outputs triangle numbers : 1, 3, 6, 10, 15, etc.

Page 44: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

44

Soal 11 (nested loop)

• Write a program which takes two integer values from the user and outputs the multiplication tables between the first and second numbers. (eg 4 and 7 will output the 4 times table, the 5 times table, the 6 times table and the 7 times table

Page 45: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

45

Soal 12

• Write a program which validate a user password. If program accepts invalid password, the output will display “invalid password, try again!”, and then program request the password again until 3 times, after 3rd iteration, the output is ”unauthorized user” . The display to user who enter valid password is “wellcome”.

Page 46: Pendalaman Materi  Conditional  dan  Repetition ( Pengulangan )

46

Soal 13

• Write a program which takes N integer values from the user and outputs the maximum.

• Illustration– N = 3• Data 1 : 15• Data 2 : 20• Data 3 : 10

– Maximum = 20