TO DO!!! COBALAH Contoh program pada slide 3-18 dibawah ini, pahami dan catat/tuliskan perbedaannya. TUGAS Latihan pada slide ke 19 no 1,2,3,5, Dikerjakan 1 kelompok dua orang (satu bangku) YANG DIKUMPULKAN HARI INI ADLH TUGAS NO.1 (SOFTCOPY) DI BAAK (1 orang mengkoordinir dikumpulkan dlm 1 folder) SEDANGKAN NO.2 DIKUMPULKAN PEKAN DEPAN
Lesson 3 – Pernyataan Berulang WHILE-DO REPEAT UNTIL FOR
WHILE Banyak perulangan belum dketahui REPEATE Idem While pemeriksaan kondisi boolean di awal, sedang repeate di akhir FOR Digunakan untuk perulangan yang sudah diketahui berapa kali akan dilakukan
WHILE DO 1 Program putaran_while_do; Uses wincrt; Var k:integer; Begin clrscr; k:=0; while k<10 do writeln(‘stmik’); k:=k+1; writeln(‘bina patria’); writeln(‘magelang’); End. 1
2 Program putaran_while_do; Uses wincrt; Var k:integer; Begin clrscr; k:=0; while k<10 do begin writeln(‘stmik’); End; k:=k+1; writeln(‘bina patria’); writeln(‘magelang’); End. 2
3 Program putaran_while_do; Uses wincrt; Var k:integer; Begin clrscr; k:=0; while k<10 do begin writeln(‘stmik’); k:=k+1; End; writeln(‘bina patria’); writeln(‘magelang’); End. 3
4 Program putaran_while_do; Uses wincrt; Var k:integer; Begin clrscr; k:=0; while k<10 do begin writeln(‘stmik’); k:=k+1; writeln(‘bina patria’); End; writeln(‘jumlah putarannya = ‘ ,k); writeln(‘magelang’); End. 4
While-do dengan kondisi boolean Program putaran_while_do_boolean; Uses wincrt; Var k:integer; Benar : boolean; Begin clrscr; benar:=true; k:=0; while benar do begin writeln(‘stmik’); k:=k+1; benar:=(k>10); writeln(‘bina patria’); End; writeln(‘magelang’); End. 5
While-do dengan kondisi boolean Program putaran_while_do_karakter; Uses wincrt; Var kar : char; Begin clrscr; writeln(‘putaran while dengan tipe karakter’); kar:='a'; while kar <=‘m’ do begin writeln(kar); kar :=succ (kar); End; writeln; Readln; End. 6
While-do dengan kondisi jamak Program putaran_while_do_jamak; Uses wincrt; Var k, A,B : integer; Begin clrscr; A:=1; B:=0; k:=1; while (A<5) and (B<5) do begin writeln(‘jumlah putaran =‘ ,k); writeln(‘Nilai A =’ ,A); writeln(‘Nilai B=’ ,B); A:=succ(A); B=succ(B); k:=succ (K); End; writeln; Readln; End. 7
Repeat Statement; … Until condition; REPEAT UNTIL Repeat Statement; … Until condition;
Program putaran_repeat_until; Uses wincrt; Var k:integer; Begin Clrscr; K:=0; Repeat writeln(‘stmik’); k:=k+1; Until k>10; Writeln (‘bina patria’); Writeln (‘magelang’); Readln; End. 8
I := 11; While I <= 10 Do Begin Writeln(‘STMIK'); Inc(I); End; Dan Repeat Until I <= 10; 9
Pada contoh 1, tidak akan mencetak apa-apa, sedangkan pada contoh 2 akan mencetak tulisan STMIK minimal 1 kali. Pada prinsipnya struktur While melakukan pemeriksaan awal, dan struktur Repeat melakukan pemeriksaan di akhir.
FOR-TO / DOWNTO-DO For variabel := nilai awal To nilai akhir Do Statement; For variabel := nilai awal DownTo nilai akhir Do Statement;
Uses wincrt; Var I : Integer; Begin For I := 1 To 10 Do Writeln(‘STMIK'); End. Uses wincrt; Var I : Integer; Begin For I := 10 DownTo 1 Do Writeln(‘STMIK'); End. 10
Break, continue, randomize Uses wincrt; Var 1:integer; begin I := 1; While I <= 10 Do Begin Writeln(‘STMIK'); I := I + 1; Break; End; End.
Break, continue, randomize Uses wincrt; Var 1:integer; begin I := 1; While I <= 10 Do Begin 20 Writeln(‘STMIK'); I := I + 1; Continue; End; End.
Break, continue, randomize num:=random(50) variabel num mungkin berisi salah satu bilangan 0 s/d 49. Uses wincrt; var i : byte; begin randomize; for i:=1 to 10 do write(random(30),' '); end.
LATIHAN 1. Buatlah program yang menghasilkan output: 1 2 3 4 5 6 2. Output 123454321
3.Output 12345 4. Output 12345 23451 34512 45123 51234 5. Output 1 12 123 1234
6. Output 11111 22222 33333 44444 55555 7.Output 1 22 333 4444 8. Output 1 121 12321 1234321 123454321 12344444321 1233333333321 122222222222221 11111111111111111 9. Output 11111 12221
If conditon Then Statement1 Else Statement2; IF THEN If conditon Then Statement1 Else Statement2;
Uses wincrt; Var Bilangan : Integer; Begin Clrscr; Write(‘Masukan Bilangan ? ‘); Readln(Bilangan); If (Bilangan Mod 2) = 0 Then Writeln (‘Genap’) Else Writeln (‘Ganjil’); Readln; End.
1 If condition1 Then Statement1 Else If condition2 Then Statement2 If condition3 Then Statement3 Statement4;
if mark>=80 then grade:='A' else { 79 or below goes here } if mark>=70 then grade:='B' else { 69 or below goes here } if mark>=60 then grade:='C' else { 59 or below goes here } if mark>=50 then grade:='D' else { 49 or below goes here } grade:='E';
2 Case variabel Of Range1 : Statement1; Range2 : Statement2; Range3 : Statement3; … Else StatementN; End;
Case nilai of 80. 100: grade:='A'; 70. 79 : grade:='B'; 60 Case nilai of 80..100: grade:='A'; 70..79 : grade:='B'; 60..69 : grade:='C'; 50..59 : grade:='D'; Else grade:='E'; End;
If-then jamak If ((tahun Mod 4) = 0) And Not (tahun Mod 100 = 0)) Or (tahun Mod 400) = 0 Then Writeln (‘Tahun Kabisat !’) Else Writeln (‘Bukan Tahun Kabisat !’);
LATIHAN OUTPUT = * age < 2 ==> "You are a baby !" * 2 to age < 12 ==> "You are a kid !" * 12 to age < 18 ==> "You are a teenager !" * 18 to age < 24 ==> "You are a young adult !" * 24 to age < 40 ==> "You are an adult !" * 40 to age < 55 ==> "You are middle aged !" * 55 to age < 65 ==> (Give comments yourself) * age to 65 ==> (Give comments yourself)
Special Reserved Words of the CRT/WINCRT Unit TAMBAHAN Special Reserved Words of the CRT/WINCRT Unit Turbo Pascal menyediakan suatu unit CRT, yang berisi perintah-perintah pengaturan layar seperti membersihkan layar, mengatur warna tulisan, warna background, memindahkan posisi cursor, mode layar, dan membuat window
Examples : Clrscr; GotoXy(_,_); Textbackground(); Textcolor(); Readkey; Delay(); Halt; / Halt()
Example of each Clrscr: (clear screen) writeln('When you press enter, the screen would be cleared!'); readln; clrscr;
Gotoxy(int,int): (Go to position x and y); gotoxy(10,10); Writeln('The position is 10 pixels from the left of the screen, and ten pixels from the top of the screen'); readln;
Readkey: (Reads a key-press); Example 1: Writeln('Press ANY key!!!'); Readkey; Example 2: Writeln('Press ANY key'); Keypress := readkey; {keypress is a DECLARED string variable(can be an integer variable)} Writeln(Keypress);
Halt(int): (Program terminates with an exit code); writeln('Press enter and the program terminates!); Readln; Halt(0); Note that instructions following 'halt' are not executed since the program terminates when halt is encountered.
Untuk crt Textbackground(word/int): (Background colour); Textbackground(red); {word - red} Writeln('Note the difference'); Textbackground(5); {integer - 5} ClrScr; Writeln('Note the difference'); Readln;
Untuk crt Textcolor(word/int): (Text colour); Textcolor(red); {word - red} Writeln('Text colour'); Textcolor(5); {integer - 5} Writeln('Text colour'); Readln;
Untuk crt Delay(int): (Waits for some time); Writeln('1'); Delay(1000);{1000 milliseconds} Writeln('2'); Delay(1000); Writeln('3'); Readln;