pemrograman pascal
MODUL PEMROGRAMAN DENGAN MENGGUNAKAN BAHASA PASCAL CONTOH PROGRAM DENGAN MENGGUNAKAN BAHASA PASCAL (FPC) 1. PENGGUNAAN MASUKAN (INPUT ) Code Program Pascal uses wincrt; {versi windows} uses crt; {turbo pascal} var fileinp: text ; A,B,C : real ; begin assign(fileinp, 'input.txt'); reset(fileinp); read(fileinp,A,B); C := A + B ; write(C) ; end.
Diagram Alir (Flow Chart) mulai
baca A dan B
C=A+ B
Input Program (dari file 'input.txt' )
tulis C
Output Program (ke monitor/LCD)
selesai
1 2 3
2. PENGGUNAAN KELUARAN (OUTPUT ) Code Program Pascal uses wincrt; {versi windows} uses crt; {turbo pascal} var fileout: text ; A,B,C : real ; begin assign(fileout, 'output.txt'); rewrite(fileout); A := 1.0 ; B := 2.0 ; C := A + B ; write(fileout,C) ; close(fileout) ; end.
Output Program (ke file 'output.txt' ) 3.00000000000000E+000
Ahmad Zakaria
Diagram Alir (Flow Chart) mulai
A=1, B=2, C = A + B
tulis C
selesai
pemrograman pascal
3. PENGGUNAAN KELUARAN (OUTPUT ) DENGAN FORMAT Code Program Pascal uses wincrt; {versi windows} uses crt; {turbo pascal} var fileout: text ; A,B,C : real ; begin assign(fileout, 'output.txt'); rewrite(fileout); A := 1.0 ; B := 2.0 ; C := A + B ; write(fileout,C:8:3) ; close(fileout) ; end.
Diagram Alir (Flow Chart) mulai
A=1, B=2, C = A + B
tulis C
selesai
Output Program (ke file 'output.txt' ) 3.000
3. PENGGUNAAN KELUARAN (OUTPUT) INTEGER Code Program Pascal uses wincrt; {versi windows} uses crt; {turbo pascal} var fileout: text ; I,J,K : integer ; begin assign(fileout, 'output.txt'); rewrite(fileout); I := 1 ; J := 2 ; K := I + J ; write(fileout,K) ; close(fileout) ; end.
Output Program (ke file 'output.txt' ) 3
Ahmad Zakaria
Diagram Alir (Flow Chart) mulai
I=1, J=2, K = I + J
tulis K
selesai
pemrograman pascal
4. PENGGUNAAN KELUARAN (OUTPUT) INTEGER DENGAN FORMAT Code Program Pascal
Diagram Alir (Flow Chart)
uses wincrt; {versi windows} uses crt; {turbo pascal} var fileout: text ; I,J,K : integer ; begin assign(fileout, 'output.txt'); rewrite(fileout); I := 1 ; J := 2 ; K := I + J ; write(fileout,K:2) ; close(fileout) ; end.
mulai
I=1, J=2, K = I + J
tulis K
selesai
Output Program (ke file 'output.txt' ) 3
5. PENGGUNAAN PERNYATAAN BERSYARAT contoh 1 Code Program Pascal uses wincrt; {versi windows} uses crt; {turbo pascal} var I,J,K : integer ; begin I := 1 ; J := 2 ; K := 3 ; if J > I then begin K := 4 ; end ; write(I:2,J:2,K:2) ; end.
Diagram Alir (Flow Chart) mulai I = 1, J = 2, K = 3
J>I ya K=4 tulis I, J, K
Output Program (ke Monitor/LCD) 1 2 4
Ahmad Zakaria
selesai
tidak
pemrograman pascal
6. PENGGUNAAN PERNYATAAN BERSYARAT contoh 2 Code Program Pascal uses wincrt; {versi windows} uses crt; {turbo pascal} var I,J,K : integer ; begin I := 2 ; J := 1 ; K := 3 ; if J > I then begin K := 4 ; end ; write(I:2,J:2,K:2) ; end.
Diagram Alir (Flow Chart) mulai I=2, J=1, K = 3
J>I
tidak
ya K=4 tulis I, J, K
Output Program (ke Monitor/LCD) selesai
1 2 3
7. PENGGUNAAN LOOPING contoh 1 Code Program Pascal uses wincrt; {versi windows} uses crt; {turbo pascal} var I : integer ; begin for I := 1 to 5 do begin write(I:2) ; end ; end.
Diagram Alir (Flow Chart)
mulai
I=1 I=I+1
I>5 tidak
tulis I
Output Program (ke Monitor/LCD) 1 2 3 4 5
Ahmad Zakaria
selesai
ya
pemrograman pascal
8. PENGGUNAAN LOOPING contoh 2 Code Program Pascal uses wincrt; {versi windows} uses crt; {turbo pascal} var I : integer ; begin for I := 1 to 5 do begin writeln(I:2) ; end ; end.
Output Program (ke Monitor/LCD) 1 2 3 4 5
Diagram Alir (Flow Chart)
mulai
I=1 I=I+1
I>5
ya
tidak tulis I
selesai
9. PENGGUNAAN LOOPING contoh 3 Code Program Pascal uses wincrt; {versi windows} uses crt; {turbo pascal} var I : integer ; begin for I := 5 downto 1 do begin write(I:2) ; end ; end.
Diagram Alir (Flow Chart)
mulai
I=5 I=I-1
I<1 tidak
tulis I
Output Program (ke Monitor/LCD) 5 4 3 2 1 selesai
Ahmad Zakaria
ya
pemrograman pascal
10. PENGGUNAAN LOOPING contoh 4 Code Program Pascal uses wincrt; {versi windows} uses crt; {turbo pascal} var I : integer ; begin for I := 5 downto 1 do begin writeln(I:2) ; end ; end.
Output Program (ke Monitor/LCD) 5 4 3 2 1
Diagram Alir (Flow Chart)
mulai
I=5 I=I-1
I<1
ya
tidak tulis I
selesai
11. PENGGUNAAN ARRAY 1 DIMENSI Code Program Pascal uses wincrt; {versi windows} uses crt; {turbo pascal} var I : integer ; A : array[1 .. 5] of real ; begin for I := 1 to 5 do begin A[I] := I ; writeln(A[I]:5:2) ; end ; end.
Output Program (ke Monitor/LCD) 1.00 2.00 3.00 4.00 5.00
Ahmad Zakaria
Diagram Alir (Flow Chart) mulai
I=1 I=I+1
I>5 tidak
A[I] = I tulis A[I]
selesai
ya
pemrograman pascal
12. PENGGUNAAN ARRAY 2 DIMENSI Code Program Pascal
Diagram Alir (Flow Chart)
uses wincrt; {versi windows} uses crt; {turbo pascal} var I,J : integer ; A : array[1 .. 2,1 .. 3] of real ; begin for I := 1 to 2 do begin for J := 1 to 3 do begin A[I,J] := I*J ; writeln(A[I,J]:5:2) ; end ; end ; end.
mulai
I=1 I=I+1
I>2
ya
tidak J=1 J=J+1
J>3
ya
tidak A[I,J] = I×J
Output Program (ke Monitor/LCD) 1.00 2.00 3.00 2.00 4.00 6.00
tulis A[I,J]
selesai
13. PENGGUNAAN SUBPROGRAM/PROCEDURE Code Program Pascal
Output Program (ke Monitor/LCD) 1.00 2.00 3.00
Ahmad Zakaria
prosedure
mulai a = 1, b = 2, c = a + b
tulis a, b, c selesai mulai
program utama
uses wincrt; {versi windows} uses crt; {turbo pascal} {prosedure} procedure hitung ; var a,b,c : real ; begin a := 1.0 ; b := 2.0 ; c := a + b ; writeln(a:5:2,b:5:2,c:5:2) ; end; {program utama} begin hitung ; end.
Diagram Alir (Flow Chart)
hitung
selesai