While … do … Repeat … until … For … to … do … Kuliah #3: Perulangan While … do … Repeat … until … For … to … do …
Iterasi Loop Mengeksekusi satu atau lebih perintah berulang-ulang
Iterasi Loop While Loop Repeat-until Loop For
Loop While While condition do statement condition False True Statement
Loop While Count Controlled Event Controlled Loop dengan jumlah tertentu Loop selesai setelah “n” kali Loop dengan jumlah bervariasi Loop selesai ketika kondisi = “False”
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
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}
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”
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.
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
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
Example 1 Calculate the “n!” (n-factorial) Start Read “n” n := n -1 n > 0 Write “fac” End False True fac := fac * n
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}
Example 2 Convert Celcius to Farenheit Celcius Farenheit 0.0 32.0 1.0 33.8 2.0 35.6 … … 99.0 210.2 100.0 212.0 Farenheit = Celcius * (9/5) + 32
Example 2 Convert Celcius to Farenheit Start Write heading cel <=100 Write output End False True Far := Cel * (9/ 5) + 32 Cel := Cel +1
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.
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”
Repeat-until Loop Repeat Statement; … Until condition Statement False True Statement
While Loop Count Controlled While n <= 5 do begin writeln(n); n := n +1; end; n <= 5 False True Writeln(n) n := n +1
Repeat-until Loop Count Controlled writeln(n); n := n +1; Until n > 5; Writeln(n) n : n +1 False n > 5 True
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;
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
For Loop Same as While loop For variable:= initial value to final value do statement For variable:= initial value downto final value do
For Loop for n := 1 to 5 do writeln(n); for n := 5 downto 1 do
Nested For-Loops for x := 1 to 3 do for y := 20 to 25 do writeln(x:5, y:5);
Example 1 Calculate the “n!” (n-factorial) Start Read “n” n := n -1 n > 0 Write “fac” End False True fac := fac * n
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.
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.
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.
Example 2 Convert Celcius to Farenheit Celcius Farenheit 0.0 32.0 1.0 33.8 2.0 35.6 … … 99.0 210.2 100.0 212.0 Farenheit = Celcius * (9/5) + 32
Example 2 Convert Celcius to Farenheit Start Write heading cel <=100 Write output End False True Far := Cel * (9/ 5) + 32 Cel := Cel +1
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.
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.
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.
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.
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.
Soal 3. Program untuk mencetak bilangan bulat sbb: 1 3 5 7 9 11 13 15 menggunakan struktur pengulangan Repeat-Until, While dan For.
Soal 4. Program untuk mencetak bilangan bulat sbb: 15 13 11 9 7 5 3 1 menggunakan struktur pengulangan Repeat-Until, While dan For.
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.
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?