410 likes | 603 Views
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 )
E N D
TO DO!!! • COBALAHContoh program padaslide3-18dibawahini,pahamidancatat/tuliskanperbedaannya. • TUGASLatihanpada slide ke 19 no 1,2,3,5, Dikerjakan 1 kelompokdua orang (satubangku) YANG DIKUMPULKANHARIINIADLHTUGAS NO.1 (SOFTCOPY) DI BAAK (1 orang mengkoordinirdikumpulkandlm 1 folder) SEDANGKAN NO.2 DIKUMPULKANPEKANDEPAN
Lesson 3 – PernyataanBerulang WHILE-DO REPEAT UNTIL FOR
WHILE Banyakperulanganbelumdketahui FOR Digunakan untuk perulangan yang sudah diketahui berapa kali akan dilakukan REPEATE Idem While pemeriksaankondisiboolean di awal, sedangrepeate di akhir
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(‘jumlahputarannya = ‘ ,k); writeln(‘magelang’); End. 4
While-do dengankondisiboolean 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 dengankondisiboolean Program putaran_while_do_karakter; Uses wincrt; Varkar : char; Begin clrscr; writeln(‘putaran while dengantipekarakter’); kar:='a'; while kar <=‘m’ do begin writeln(kar); kar :=succ (kar); End; writeln; Readln; End. 6
While-do dengankondisijamak 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(‘jumlahputaran =‘ ,k); writeln(‘Nilai A =’ ,A); writeln(‘Nilai B=’ ,B); A:=succ(A); B=succ(B); k:=succ (K); End; writeln; Readln; End. 7
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 I := 11; Repeat Writeln(‘STMIK'); Inc(I); Until I <= 10; 9
Padacontoh 1, tidakakanmencetakapa-apa, sedangkanpadacontoh 2 akanmencetaktulisanSTMIKminimal 1 kali. • Padaprinsipnyastruktur While melakukanpemeriksaanawal, danstrukturRepeat melakukanpemeriksaan di akhir.
FOR-TO / DOWNTO-DO For variabel := nilaiawalTo nilaiakhirDo Statement; For variabel := nilaiawalDownTonilaiakhirDo Statement;
Uses wincrt; VarI : Integer; Begin For I := 1 To 10 Do Writeln(‘STMIK'); End. Uses wincrt; VarI : 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) variabelnummungkinberisisalahsatubilangan0 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 12345 12345 12345 12345 4. Output 12345 23451 34512 45123 51234 5. Output 1 12 123 1234 12345
6. Output 11111 22222 33333 44444 55555 7.Output 1 22 333 4444 55555 8. Output 1 121 12321 1234321 123454321 12344444321 1233333333321 122222222222221 11111111111111111 9. Output 11111 12221 12321 12221 11111
IF THEN If conditon Then Statement1 Else Statement2;
Uses wincrt; Var Bilangan : Integer; Begin Clrscr; Write(‘MasukanBilangan ? ‘); 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 Else If condition3 Then Statement3 Else 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 CasevariabelOf Range1 : Statement1; Range2 : Statement2; Range3 : Statement3; … Else StatementN; End;
Case nilaiof 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 (‘TahunKabisat !’) Else Writeln (‘BukanTahunKabisat !’);
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)
TAMBAHAN Special Reserved Words of the CRT/WINCRT Unit Turbo Pascal menyediakan suatu unit CRT, yang berisi perintah-perintah pengaturan layarsepertimembersihkanlayar, mengaturwarnatulisan, warna background, memindahkanposisi cursor, mode layar, danmembuat 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.
Untukcrt Textbackground(word/int): (Background colour); Textbackground(red); {word - red}Writeln('Note the difference');Textbackground(5); {integer - 5}ClrScr;Writeln('Note the difference'); Readln;
Untukcrt Textcolor(word/int): (Text colour); Textcolor(red); {word - red}Writeln('Text colour');Textcolor(5); {integer - 5}Writeln('Text colour'); Readln;
Untukcrt Delay(int): (Waits for some time); Writeln('1');Delay(1000);{1000 milliseconds}Writeln('2');Delay(1000);Writeln('3');Readln;