kuliah 2 - pendahuluan pascal
TRANSCRIPT
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
1/34
Pendahuluan PASCAL
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
2/34
Apakah Pascal itu?
Pascal adalah Bahasa pemrogramantingkat tinggi
Mudah dimengerti karena menggunakan
sintaks Bhs. Inggris sederhana sepertiBEGIN, END, READ, WRITE, IF, THEN,REPEAT, WHILE, DO, dsb.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
3/34
Apakah Pascal itu?
Pascal
Dibuat oleh Nicklaus Wirth (Swiss) pada akhirtahun 60an
Mudah dipelajari, mudah digunakan, ...
Tersedia secara gratis
Pelopor bahasa pemrograman terstruktur
Dapat berbentuk modular
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
4/34
Apakah Pascal itu?
Kompiler yang banyak dikenal:
Turbo Pascal, Borland Pascal, TPW, FP
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
5/34
Struktur Program Pascal
Setiap program Pascal memuat headerdan blok.
Header dimulai dengan kata PROGRAM,yang diikuti oleh nama program.
Blok memiliki dua bagian utamabagiandeklarasi dan bagian pernyataan.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
6/34
Struktur Program Pascal
Bagian deklarasi mendefiniskan variable,konstanta, penggunaan unit, fungsi,prosedur dsb.
Bagian pernyataan memuat pernyataansebenarnya yang mengakibatkan aksi
yang akan dilakukan komputer Sebuahprogram memuat paling kurang satu barispernyataan.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
7/34
Contoh 1
PROGRAM Pertama;
BEGIN
WRITELN(Selamat Pagi);END.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
8/34
Contoh 2
PROGRAM Kedua;
CONST nama=Khaeruddin;
BEGINWRITELN(Selamat Pagi ,nama);
WRITELN(Apa kabar hari ini);
END.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
9/34
9
Contoh 3
Program Ketiga;
const
NIM = 1234567;
varmatakuliah: integer;
begin
write(Masukkan jumlah MK yang anda programkan);
read(matakuliah);writeln(Mahasiswa, NIM, mengambil , matakuliah, matakuliahsemester ini);
end.
Program Heading
Declarasi
Program Body
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
10/34
10
Reserved Words
program Ketiga;
const
NIM = 1234567;
var
matakuliah: integer;
begin
write(Masukkan jumlah MK yang anda programkan);
read(matakuliah
);
writeln(Mahasiswa, NIM, mengambil , matakuliah, matakuliahsemester ini);
end.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
11/34
Contoh 4
PROGRAM lingkaran; (*Header*)
(*This program calculate and print the area of a circle.*)
VAR luas, jejari:real; (*Variable Declaration*)
BEGIN
read(jejari); (*Statement*)
luas :=3.14159 * (jejari * jejari); (*Statement*)
write(luas); (*Statement*)
END.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
12/34
Pascal Fundamentals OPERATOR ARITMETIKA
+ Penjumlahan
- Pengurangan
* Perkalian
/ Pembagian bil. ril
DIV Pembagian bernilai bulat
MOD Sisa hasil bagi
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
13/34
Aktivitas Dengan menggunakan OPERATOR ARITMETIKA
Tulis Sintaks Pascal untuk pernyataan berikut:
Menghitung sisa hasil bagi 20 dibagi 6, dan simpan dalam variable yangdisebut hasil.
hasil := 20 MOD 6;
Simpan dalam variable hasiljumlah 3 bilangan yang disimpan dalamvariabel n1, n2, n3 dibagi 3.
hasil := (n1 + n2 + n3) DIV 3;
Simpan dalam variable keliling, keliling suatu lingkaran jika diketahuikonstanta pidan jari-jari radius.
keliling := 2 * pi * radius;
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
14/34
Fundamental Pascal
INDENTIFIER Sebuah identifier adalah sebuah nama yang diberikan
ke beberapa elemen program seperti namakonstanta, nama variable, nama prosedur, atau namaprogram
Identifiers terdiri atas huruf atau angka, dalam urutansembarang, yang harus dimulai dengan huruf.
Huruf besar maupun kecil dibolehkan.
Karakter selain huruf dn angka tidak dibolehkan.
Spasi kosong tidak dibolehkan (ingat, spasi kosongjuga karakter)
Panjang masksimum 63 karakter
Boleh menggunakan underscore
Contoh: studentName, Parent_Name
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
15/34
Aktivitas
Tentukan yang mana yang DIBOLEHKAN atau TIDAK
DIBOLEHKAN
3grade NOT ALLOWED solution: grade3
StudentNAMe ALLOWED
Book Number NOT ALLOWED solution: Book_Number
Money$ NOT ALLOWED solution: Money
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
16/34
Pascal Fundamentals DATA TYPES
One of the most important and interesting characteristics of Pascal is itsability to support many different types of data. You will be exposed to three.
Simple-type Data
These are single items (numbers, characters) that are associated withsingle identifiers on a one-to-one basis.
There are several different simple data types. These include the fourstandard data typesinteger, real, char and bo olean.
String-type Data
This represent strings of characters.
Structured-type Data
This consist of multiple data items that are related to one another insome specified manner. Each group of data is associated with aparticular identifier. An example is Array (will be discussed later).
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
17/34
Pascal Fundamentals CONSTANTS
It is often convenient to associate a simple data item, such as anumerical value or a string, with an identifier, thus providing a name forthe data item. The identifier is called a constant if the data item isassigned permanently (ie. If the data item remains the same throughoutthe program).
A constant must always be defined before it can appear in Pascalstatement.
Example: Two items that are constant GCT and PI.
CONST gct = 17.5;
CONST pi = 3.14128
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
18/34
Pascal Fundamentals VARIABLES
An identifier whose value is allowed to change during the execution of aprogram is called a variable.
Every variable must be individually declared (defined)before it can beused in a program.
The variable declaration establishes the fact that the identifier is avariable (rather than a constant) and specifies the type of the variable.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
19/34
Pascal Fundamentals ACTIVITY
1. Declare two variables, num1 and num2 to hold whole numbers.
VAR num1, num2: integer;
2. Declare a variable studentname.
VAR studentname: string;
3. Declare two variables to hold the price and name of an item.
VAR itemname: string;
VAR itemprice: real;
4. Declare a variable to hold an answer of Y for yes OR N for no.VAR answer: char;
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
20/34
Pascal Fundamentals RELATIONAL OPERATORS (used to make comparisons)
= equal to
not equal to
< less than greater than
>= greater than or equal to
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
21/34
Data Input and Output THE READ STATEMENT
The read statement is used to read data items from the input data fileand assign them to integer, real, char or string-type variables.
The statement is written as
read ( variablename);
Input values can be separated by commas.
THE READLN STATEMENT
The readln statement, like the read statement, is used to read dataitems from the input file and assign them to integer, real, char or stringvariables.
The difference between the two statements is the readln cause thefollowing (not the current) read or readln statement to begin by readinga new line of data, whereas the read statement will allow the followingread or readln statement to begin on the same line.
Example: readln (variablename);
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
22/34
Data Input and Output THE WRITE STATEMENT
The write statement is used to write data items to the screen.
An example of this statement is written as:
write( x =, x);
THE WRITELN STATEMENT
The writeln statement is identical to the write statement, except that the writelnstatement results in an end-of-line designation being written after the last item.Therefore, any subsequent write or writeln statement will begin a new line ofoutput.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
23/34
Pseudocode to Pascal
MULAI
DEKLARASIKAN price, newprice: realCONSTANT gct 17.5
CETAK Enter the priceINPUT price
newprice (price * (gct /100)) + price
CETAK The new price is, newprice
SELESAI
Program gettingprice;
VAR price, newprice : real;
CONST gct = 17.5;
Beginwrite(Enter the Price);
read(price);
newprice := (price * (gct /100)) + price;
write (The new price is, newprice);End.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
24/34
Pseudocode to Pascal
START
DECLARE age, fare AS integer
PRINT Enter your age
READ age
IF age >= 18 THENfare 50
ELSEfare 15
ENDIFPRINT Your bus fare is $, fare
STOP
Program adultchild;
VAR age : integer;
Begin
write(Enter your age);read(age);
IF age >= 18 THEN
begin
write(You are an adult);
endELSE
begin
write (You are a child);
end;
End.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
25/34
Pseudocode to Pascal
START
DECLARE grade AS realDECLARE studentname AS string
PRINTWhat is your name?
READ studentname
PRINT Enter the gradeREAD grade
WHILE grade < 60 DOPRINT studentname, will redo test!
PRINT What is the new grade?READ grade
ENDWHILE
PRINT Congrats,, studentname, !!!
STOP
Program studentinfo;
VAR grade : real;
VAR studentname : string;
Begin
write(What is your name?);
read (studentname);
write(Enter the grade);
read (grade);
WHILE grade < 60 DO
begin
write(studentname, will redo test!);
write(What is the new grade?);
read(grade);
end;write (Congrats,, studentname,!!!);
End.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
26/34
Pseudocode to Pascal
START
DECLARE studentname AS string
FOR count 1 TO 30 DO
PRINTWhat is your name?READ studentname
ENDFOR
PRINT You have entered all 30 namesSTOP
Program gettingnames;
VAR studentname : string;
Begin
FOR count := 1 TO 30 DO
beginwrite(What is your name?);
read (studentname);
end;write (You have entered all 30 names);
End.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
27/34
LATIHAN
Soal 1. Buatlah program untukmenuliskan kalimat AssalammualikumwR. WB ke layar.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
28/34
Soal 2. Buatlah program untukmendeklarasikan sebuah variabel bertipeinteger dan isilah variabel tersebut
dengan cara assignment kemudianmencetak nilai variabel tersebut. Cobalahassignment variabel tersebut dengan
bilangan real, apa yang terjadi, amatilah!!!
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
29/34
Soal 3, Soal4, Soal5. Idem no 2, dengantipe data lainnya (real, karakter, boolean).Cobalah untuk mengisi variabel dengan
nilai yang berbeda dengan tipe datavariabel tersebut !!!
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
30/34
Soal 6. Dibaca bilangan yangmenyatakan tinggi badan seseorangdalam centimeter. Nilai tinggi badan
dimungkinkan dalam pecahan (100,4 ;160,67 ; 120, 45). Buatlah program yangtepat untuk mendeklarasikan variabel,
mengisi variabel dan mencetak isi variabelsesuai permasalahan tersebut.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
31/34
Soal 7. Buatlah program yangmendeklarasikan dua bilangan bertipereal. Input bilangan bertipe real tersebut
dari keyboard, lakukan operasi tambahkurangbagikali- kurangdari-lebihdari.Tampilkan hasil dari setiap operasi ke
layar.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
32/34
Soal 8. Idem dengan soal no 2. MisalL=65.5678 maka penulisan luas lingkarandi layar adalah sbb : 65.57 ( lebar
tulisan adalah 9 digit dengan 2 digit dibelakang koma)
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
33/34
Soal 9. Bool1 dan Bool2 adalah variabelyang masing-masing berisi nilai TRUE danFALSE. Buatlah program untuk melakukan
operasi AND, OR, XOR, NOT terhadapvariabel tersebut.
-
8/12/2019 Kuliah 2 - Pendahuluan Pascal
34/34
Soal 10. Diketahui nilai variabel n= 8(desimal). Buatlah program untukmenggeser bit bilangan 8 dalam desimal
sebanyak 2 ke kiri dan 3 ke kanan..