Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

While … do … Repeat … until … For … to … do …

Presentasi serupa


Presentasi berjudul: "While … do … Repeat … until … For … to … do …"— Transcript presentasi:

1 While … do … Repeat … until … For … to … do …
Kuliah #3: Perulangan While … do … Repeat … until … For … to … do …

2 Iterasi Loop Mengeksekusi satu atau lebih perintah berulang-ulang

3 Iterasi Loop While Loop Repeat-until Loop For

4 Loop While While condition do statement condition False True Statement

5 Loop While Count Controlled Event Controlled
Loop dengan jumlah tertentu Loop selesai setelah “n” kali Loop dengan jumlah bervariasi Loop selesai ketika kondisi = “False”

6 While Loop :Count Controlled
While n <= 5 do begin writeln(n); n := n +1; end; {while} n = 0; n <= 5 False True Writeln(n) n := n +1

7 Event Controlled: Boolean Flag
Rain := True False True Readln(n) n > 0 writeln(n) Rain := False rain := True; While rain do begin readln (n); if n > 0 then writeln(n) else rain := False end; {while}

8 Event Controlled:Sentinel Value
Read on or more data in each loop When should we stop reading ? Instruct user to enter a unique number called “Sentinel Value”

9 Event Controlled:Sentinel Value
Program test; Const Stop_Value = -999; Var num,sum: integer; Begin Readln(num); while num <> Stop_value do begin sum := sum + num; Readln (num); end; {while} writeln(sum); End.

10 While Loop Count Controlled Event Controlled Exact number of loops
Exit loop after “n” times Vary number of loops Exit loop when condition = “False” condition = Sentinel Value

11 Number of loop Exact number of loop Vary number of loop
The least number of loop ? Example: Readln (n); While n <= 5 do begin writeln(n); n := n +1; end; {while} NONE

12 Example 1 Calculate the “n!” (n-factorial)
Start Read “n” n := n -1 n > 0 Write “fac” End False True fac := fac * n

13 n! program begin fac := 1; write(‘Please input n ’); readln(n);
while n > 0 do fac := fac * n; n := n –1; end; {while} writeln(n, ‘! = ‘, fac) end. {main}

14 Example 2 Convert Celcius to Farenheit
Celcius Farenheit … … Farenheit = Celcius * (9/5) + 32

15 Example 2 Convert Celcius to Farenheit
Start Write heading cel <=100 Write output End False True Far := Cel * (9/ 5) + 32 Cel := Cel +1

16 Convert Celcius to Farenheit program
begin Writeln(‘Celcius’:10, ‘Farenheit’:15); Cel := 0; while Cel <= 100 do Far := Cel * (9/ 5) + 32; writeln(cel:10:1, farenheit:15:1); Cel := Cel + 1 end {while} end.

17 Repeat-Until Loop Count Controlled Event Controlled
Exact number of loops Exit loop after “n” times Vary number of loops Exit loop when condition = “False”

18 Repeat-until Loop Repeat Statement; … Until condition Statement False
True Statement

19 While Loop Count Controlled
While n <= 5 do begin writeln(n); n := n +1; end; n <= 5 False True Writeln(n) n := n +1

20 Repeat-until Loop Count Controlled
writeln(n); n := n +1; Until n > 5; Writeln(n) n : n +1 False n > 5 True

21 While Loop Event Controlled
Rain := True False True Readln(n) n > 0 writeln(n) Rain := False rain := True; While rain do begin readln (n); if n < 0 then writeln(n) else rain := False end;

22 Repeat-Until Loop Event Controlled
rain := True; Repeat readln (n); if n < 0 then writeln(n) else rain := False Until (rain = False); Readln(n) False n < 0 True writeln(n) Rain := False False Rain := False True

23 For Loop Same as While loop
For variable:= initial value to final value do statement For variable:= initial value downto final value do

24 For Loop for n := 1 to 5 do writeln(n); for n := 5 downto 1 do

25 Nested For-Loops for x := 1 to 3 do for y := 20 to 25 do
writeln(x:5, y:5);

26 Example 1 Calculate the “n!” (n-factorial)
Start Read “n” n := n -1 n > 0 Write “fac” End False True fac := fac * n

27 n! program begin fac := 1; write(‘Please input n ’); readln(n);
while n > 0 do fac := fac * n; n := n –1; end; writeln(n, ‘! = ‘, fac) end.

28 n! program Repeat-Until
begin fac := 1; write(‘Please input n ’); readln(n); Repeat fac := fac * n; n := n –1; end; Until (n <= 0); writeln(n, ‘! = ‘, fac) end.

29 n! program For Begin fac := 1; write(‘Please input n ’); readln(n);
For i := n downto 1 do fac := fac * i; writeln(n, ‘! = ‘, fac) end.

30 Example 2 Convert Celcius to Farenheit
Celcius Farenheit … … Farenheit = Celcius * (9/5) + 32

31 Example 2 Convert Celcius to Farenheit
Start Write heading cel <=100 Write output End False True Far := Cel * (9/ 5) + 32 Cel := Cel +1

32 Convert Celcius to Farenheit program
begin Writeln(‘Celcius’:10, ‘Farenheit’:15); Cel := 0; while Cel <= 100 do Far := Cel * (9/ 5) + 32; writeln(cel:10:1, farenheit:15:1); Cel := Cel + 1 end end.

33 Convert Celcius to Farenheit program – Repeat-Until
begin Writeln(‘Celcius’:10, ‘Farenheit’:15); Cel := 0; repeat Far := Cel * (9/ 5) + 32; writeln(cel:10:1, farenheit:15:1); Cel := Cel + 1 end until Cel > 100; end.

34 Convert Celcius to Farenheit program -- for
begin Writeln(‘Celcius’:10, ‘Farenheit’:15); for Cel :=0 to 100 do Far := Cel * (9/ 5) + 32; writeln(cel:10:1, farenheit:15:1) end end.

35 Soal 1. Program untuk mencetak bilangan bulat dari 1 s/d N menggunakan struktur pengulangan Repeat-Until, While dan For. Nilai N diinputkan dari keyboard.

36 Soal 2. Program untuk mencetak bilangan bulat dari N s/d 1 menggunakan struktur pengulangan Repeat-Until, While dan For. Nilai N diinputkan dari keyboard.

37 Soal 3. Program untuk mencetak bilangan bulat sbb: menggunakan struktur pengulangan Repeat-Until, While dan For.

38 Soal 4. Program untuk mencetak bilangan bulat sbb: menggunakan struktur pengulangan Repeat-Until, While dan For.

39 Soal 5. Program untuk memasukan bilangan yang lebih besar dari nol
Soal 5. Program untuk memasukan bilangan yang lebih besar dari nol. Program akan terus menerus minta inputan jika nilai yang dimasukan lebih kecil atau samadengan nol.

40 Soal 6.   Ada katak masuk kedalam sumur sedalam 31.2 m. 5 hari pertama katak bisa naik 3 m tiap pagi dan merosot 1 m tiap malam, 10 hari berikutnya katak bisa naik 2 m tiap pagi dan merosot 1.25 m tiap malam, selanjutnya naik 2 m tiap pagi dan merosot 1.5 m tiap malam, berapa hari katak tersebut dapat keluar dari sumur?


Download ppt "While … do … Repeat … until … For … to … do …"

Presentasi serupa


Iklan oleh Google