Logical – IF – CASE OF

1: Program Tutorial2_if;
2: var pilih: Integer;
3: Begin
4:   write('Pilih 1 atau 2'); readln(pilih);
5:   if pilih=1 then write('Anda memilih 1')
6:   elseif pilih=2 then write('Pilihan anda 2')
7:   else write('Anda memilih yang lain');
8: End.

 1: Program Tutorial2_case;
 2: var pilih: char;
 3: Begin
 4:   write('Pilih 1 atau 2'); readln(pilih);
 5:   case pilih of
 6:     '1': write('Anda memilih 1');
 7:     '2': write('Pilihan anda 2');
 8:   else write('Anda memilih yang lain');
 9:   end;
10: End.

Program CaseOf_Or;
Uses crt;
Var
Hargaperkg:integer;
Kualitas:char;
Begin
Clrscr;
Write(‘ Massukkan Kualitas Buah [A/B/C] : ‘); readln(kualitas);
Case kualitas of
‘A’,’a’: hargaperkg:=1000;
‘B’,’b’: hargaperkg:=750;
‘C’,’c’: hargaperkg:=500;
Else
Hargaperkg:=0;
End;
Writeln(‘ Harga Per Kg : Rp.’,hargaperkg);
readln;
End.

Structure Case...Of;

In the 1970s, programming language designers were looking for simpler ways to write statements that were previously involved expressions. Since the programmers made more errors on complex statements, it seemed reasonable to attempt to distill the BLOCK-IF into a more concise representation. In PASCAL, this representation is called the CASE statement. Instead of relational operators, the CASE statement simply specifies possible values for a variable, together with statements to be executed if that variable has a given value. For example, consider the following CASE statement (written in pseudocode) for a academic grade evaluation:

CASE OF grade:

‘A’: WRITELN(‘Excellent work!’);

‘B’: WRITELN(‘You did well…’);

‘C’: WRITELN(‘Average performance.’);

‘D’: WRITELN(‘Needs some improvement…’);

‘E’: WRITELN(‘Trouble ahead!’);

END-CASE

Certainly this simple instance of the CASE statement is easier to read than the corresponding BLOCK-IF statement:

IF grade == ‘A’ THEN WRITELN(‘Excellent work!’)

ELSEIF grade == ‘B’ THEN WRITELN(‘You did well…’)

ELSEIF grade == ‘C’ THEN WRITELN(‘Average performance.’)

ELSEIF grade == ‘D’ THEN WRITELN(‘Needs some improvement…’)

ELSEIF grade == ‘E’ THEN WRITELN(‘Trouble ahead!’);

ENDIF

The problem with CASE statements occurs not when the statement blocks are small, or when there are a few alternatives. Rather, the CASE statement begins to defeat its design goal of conciseness and readability when the blocks of statements become large or when there are many alternatives. In such expressions, a given CASE statement may span several pages or more. When you are trying to debug such statements, it becomes very difficult to remember where you are in the statement, and one tends to feel lost. As a result, we prefer to use only IF or BLOCK-IF statements for decision structures.

(http://www.cise.ufl.edu)


The Simple Case Statement

So far, you have learned how to use an ‘if statement’. But in some cases the ‘case statement‘ is preferred to the if statement because it reduces some unnecessary code but the same meaning is retained. The case statement is very similar to the if statement, except in that the it does not accept literal conditional expressions (i.e.: strings) but surprisingly enough, it allows single character conditional expressions. Here is how it works:

Case {variable of type: integer or character ONLY} of

{input statement- within inverted commas if of type char} : {code..}

{input statement- within inverted commas if of type char} : {code..}

End; {End Case}

http://pascalprogramming.byethost15.com

Looping

1: Program Tutorial3_for;
2: var i: Integer;
3: Begin
4:   For i:=1 to 5 do
5:     writeln('ini yang ke ', i);
6: End.

 1: Program Tutorial3_while;
 2: var i: Integer;
 3: Begin
 4:   i:=1;
 5:   while i < 5 do
 6:   begin
 7:     writeln('ini yang ke ', i);
 8:     i:=i+1;
 9:   end;
10: End.

1: Program Tutorial3_repeat;
2: var pilih: char;
3: Begin
4:   i:=1;
5:   repeat
6:     writeln('ini yang ke ',i);
7:     i:=i+1;
8:   until i<0;
9: End.

Procedure

USES CRT;
CONST
     max = 10;
     garis = '----------------------------------------------------------------';
 TYPE
     rec_data = record;
     nip : string[25];
     nama : string[25];
     gol : string[5];
     gapok : real;
     tunj : real;
     gator : real; 
     pot : real;
     gaber : real;
 END;
 VAR
     rpeg : rec_data;
     peg : file of rec_data;
     lagi : char;

 PROCEDURE entrydata;
   BEGIN
     ASSIGN (peg,'c:\peg.dat');
     REWRITE (peg);
     CLRSCR;
       BEGIN
          CLRSCR;
          WRITE ('nip :');READLN(rpeg.nip);
          IF rpeg.nip=" THEN HALT;
          WRITE ('nama :');READLN(rpeg.nama);
          WRITE ('golongan :');READLN(rpeg.gol);
       END;
   END; 
   {Akhir procedure entrydata} 

 PROCEDURE prosesdata;
   BEGIN
     IF rpeg.gol = 'I/a' THEN rpeg.gapok :=250000;
     IF rpeg.gol = 'I/b' THEN rpeg.gapok :=300000;
     IF rpeg.gol = 'II/a' THEN rpeg.gapok :=350000;
     IF rpeg.gol = 'II/b' THEN rpeg.gapok :=400000;
     IF rpeg.gol = 'III/a' THEN rpeg.gapok :=450000; 
     IF rpeg.gol = 'III/b' THEN rpeg.gapok :=500000;
     rpeg.tunj :=(0.1)*rpeg.gapok;
     rpeg.gator :=rpeg.gapok*rpeg.tunj;
     rpeg.pot :=(0.02)*rpeg.gator;
     rpeg.gaber :=rpeg.gator-rpeg.pot;
   END;
   {Akhir procedure prosesdata}

 PROCEDURE tampildata; 
   BEGIN
     CLRSCR;
     GOTOXY (1,1);WRITE (' daftar gaji pegawai ');
     GOTOXY (1,2);WRITE (garis);
     GOTOXY (1,3);WRITE (' nip nama gol gapok tunj
gator pot gaber');
     GOTOXY (1,4);WRITE (garis);
     BEGIN
          GOTOXY (2,5) ;WRITELN (rpeg.nip);
          GOTOXY (9,5) ;WRITELN (rpeg.nama);
          GOTOXY (18,5);WRITELN (rpeg.gol);
          GOTOXY (24,5);WRITE (rpeg.gapok:10:0);
          GOTOXY (33,5);WRITE (rpeg.tunj:10:0);
          GOTOXY (44,5);WRITE (rpeg.gator:10:0);
          GOTOXY (54,5);WRITE (rpeg.pot:10:0);
          GOTOXY (68,5);WRITE (rpeg.gaber:10:0);
     END;
   END;
   {Akhir procedure tampildata}

 BEGIN {Program utama}
     CLRSCR;
     REPEAT
          entrydata;
          prosesdata;
          tampildata;
          WRITELN;
          WRITE (' coba lagi : [Y/T]:');
          REPEAT
             lagi:=UPCASE (READKEY);
          UNTIL lagi IN ['Y','T'];
     UNTIL lagi ='T';
     CLOSE (peg);
 END.

Function

{ buat fungsi yang menentukan nilai terbesar }
{ dari 2 buah bilangan }

  USES CRT;
  VAR
	  A : interger;
	  B : interger;
	  C : interger;
  FUNCTION nilaiterbesar (na,nb:interger):interger;
	  BEGIN
		   IF na>nb THEN
			   nilaiterbesar:=na
		   ELSE
			   nilai terbear:=nb;
	  END;
  BEGIN
	  CLRSCR;
	  WRITE ('Masukan nilai A:');
	  READLN (A);
	  WRITE ('Masukan nilai B:');
	  READLN (B);
	  C:=nilaiterbesar (A,B);
	  WRITELN ('NILAI TERBESAR = ',C );
  END.

Structure Pascal – Struktur Program PASCAL

Bahasa Pascal memiliki struktur sbb:

PROGRAM NamaProgram (FileList);
CONST
(* pendeklarasian konstanta *)
TYPE
(* pendeklarasian tipe *)
VAR
(* pendeklarasian variabel *)
(* pendefinisian subprogram-procedure atau function *)

BEGIN
(* batang tubuh/ main body *)
END.

Susunan/ urutan struktur di atas harus dipenuhi. Meskipun demikian, kita dapat menghilangkan beberapa bagian struktur apabila tidak diperlukan.

Untuk memberikan komentar dalam PASCAL, digunakan perintah { komentar } atau (* komentar
*).

Type Data Record

(*contoh pertama:*)

uses crt;
type
alamat = record
jln : string[20];
no : string[3];
kota : string[20];
kdpos : string[6];
end;
datamhs = record
nama : string[15];
npm : string[8];
almt : alamat;
jk : char;
gldrh : string[2];
end;
var
dataku : datamhs;
begin
clrscr;
with dataku, almt do
begin
write(‘Nama : ‘); readln(nama);
write(‘NPM : ‘); readln(npm);
writeln(‘ Alamat ‘);
write(‘Jalan : ‘); readln(jln);
write(‘Nomor : ‘); readln(no);
write(‘Kota : ‘); readln(kota);
write(‘Kode Pos : ‘); readln(kdpos);
write(‘Jenis Kelamin (L/P) : ‘); readln(jk);
write(‘Golongan Darah : ‘); readln(gldrh);
end;
clrscr;
with dataku, almt do
begin
gotoxy(9,5);writeln(‘ B I O D A T A K U ‘);
gotoxy(7,7);writeln(‘Nama Saya ‘,nama);
gotoxy(7,8);writeln(‘NPM Saya ‘,npm);
gotoxy(7,9);writeln(‘Jenis Kelamin ‘,jk);
gotoxy(7,10);writeln(‘Golongan Darah ‘,gldrh);
gotoxy(7,11);writeln(‘Alamat Saya : Jalan ‘,jln,’ Nomor ‘,no,’ ,kota,’ ‘,kdpos);
end;
readln;
end.

http://one.indoskripsi.com/judul-skripsi-tugas-makalah/algoritma/tugas-algoritma-2

(*contoh kedua:

Passing Records as Arguments*)

Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;

Procedure EnterNewBook(var newBook : TBookRec);
Begin
Writeln(‘Please enter the book details: ‘);
Write(‘Book Name: ‘);
Readln(newBook.Title);
Write(‘Author: ‘);
Readln(newBook.Author);
Write(‘ISBN: ‘);
Readln(newBook.ISBN);
Write(‘Price: ‘);
Readln(newBook.Price);
End;

Procedure DisplayBookDetails(myBookRec : TBookRec);
Begin
Writeln(‘Here are the book details:’);
Writeln;
Writeln(‘Title: ‘, myBookRec.Title);
Writeln(‘Author: ‘, myBookRec.Author);
Writeln(‘ISBN: ‘, myBookRec.ISBN);
Writeln(‘Price: ‘, myBookRec.Price);
End;

Var
bookRec : TBookRec;

Begin
EnterNewBook(bookRec);
Writeln(‘Thanks for entering the book details’);
DisplayBookDetails(bookRec);
Readln;
End.

(*contoh ketiga:

Array Of Records*)

Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;

Procedure EnterNewBook(var newBook : TBookRec);
Begin
Writeln(‘Please enter the book details: ‘);
Write(‘Book Name: ‘);
Readln(newBook.Title);
Write(‘Author: ‘);
Readln(newBook.Author);
Write(‘ISBN: ‘);
Readln(newBook.ISBN);
Write(‘Price: ‘);
Readln(newBook.Price);
End;

Var
bookRecArray : Array[1..10] of TBookRec;
i : 1..10;

Begin
For i := 1 to 10 do
EnterNewBook(bookRecArray[i]);
Writeln(‘Thanks for entering the book details’);
Write(‘Now choose a record to display from 1 to 10: ‘);
Readln(i);
Writeln(‘Here are the book details of record #’,i,’:’);
Writeln;
Writeln(‘Title: ‘, bookRecArray[i].Title);
Writeln(‘Author: ‘, bookRecArray[i].Author);
Writeln(‘ISBN: ‘, bookRecArray[i].ISBN);
Writeln(‘Price: ‘, bookRecArray[i].Price);
Readln;
End.

(*contoh keempat:

Binary File and Records*)

Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;

Procedure EnterNewBook(var newBook : TBookRec);
Begin
Writeln(‘Please enter the book details: ‘);
Write(‘Book Name: ‘);
Readln(newBook.Title);
Write(‘Author: ‘);
Readln(newBook.Author);
Write(‘ISBN: ‘);
Readln(newBook.ISBN);
Write(‘Price: ‘);
Readln(newBook.Price);
End;

Var
bookRecArray : Array[1..10] of TBookRec;
tempBookRec : TBookRec;
bookRecFile : File of TBookRec;
i : 1..10;

Begin
Assign(bookRecFile, ‘bookrec.dat’);
ReWrite(bookRecFile);
For i := 1 to 10 do
Begin
EnterNewBook(bookRecArray[i]);
{ bookRecArray[i] now contains the book details }
Write(bookRecFile, bookRecArray[i]);
End;
Close(bookRecFile);
Writeln(‘Thanks for entering the book details.’);
Writeln(‘They are saved in a file!’);
Write(‘Now choose a record to display from 1 to 10: ‘);
Readln(i);
ReSet(bookRecFile);
Seek(bookRecFile, i-1);
Read(bookRecFile, tempBookRec);
Close(bookRecFile);
Writeln(‘Here are the book details of record #’,i,’:’);
Writeln;
Writeln(‘Title: ‘, tempBookRec.Title);
Writeln(‘Author: ‘, tempBookRec.Author);
Writeln(‘ISBN: ‘, tempBookRec.ISBN);
Writeln(‘Price: ‘, tempBookRec.Price);
Readln;
End.

http://pascalprogramming.byethost15.com

Algoritma, Algorism, Algorithm

ALGORITMA

I. Asal Usul Kata Algoritma
Kata algoritma berasal dari kata-kata di bawah ini :
Al Khuwarizmi -> algorism ->algorithm (diserap dalam bahasa Indonesia menjadi
algoritma)
· Abu Ja’far Muhammad Ibnu Musa Al Khuwarizmi adalah seorang penulis buku Arab
yang berjudul Kitab Al Jabar Wal Muqabala (Buku Pemugaran dan Pengurangan).
Kata Al Khuwarizmi dibaca orang Barat menjadi algorism.
· Kata algorism berarti proses menghitung dengan angka Arab [1]. Seseorang
dikatakan algorist jika orang tersebut menggunakan angka Arab.
· Kata algorism lambat laun menjadi algorithm disebabkan kata algorism sering
dikelirukan dengan kata arithmetic sehingga akhiran –sm berubah menjadi –thm. Kata
algorithm diserap ke dalam bahasa Indonesia menjadi algoritma.

II. Definisi Algoritma
Algoritma adalah urutan langkah-langkah logis penyelesaian masalah yang disusun
secara sistematis [1].
Kata logis disini berarti benar sesuai dengan logika manusia. Untuk menjadi sebuah
algoritma, urutan langkah yang ditempuh untuk menyelesaikan masalah harus
memberikan hasil yang benar.

Contoh :

Algoritma MENUKAR_ISI_BEJANA
{Diberikan dua buah bejana A dan B. Bejana A berisi larutan berwarna merah, bejana B
berisi larutan berwarna biru.}
1. Tuangkan larutan dari bejana A ke dalam bejana C.
2. Tuangkan larutan dari bejana B ke dalam bejana A.
3. Tuangkan larutan dari bejana C ke dalam bejana B.

Sedangkan yang berikut ini adalah urutan langkah untuk menukar isi bejana yang tidak
logis :

1. Tuangkan larutan dari bejana A ke dalam bejana B.
2. Tuangkan larutan dari bejana B ke dalam bejana A.

III. Berbagai Contoh Algoritma

Algoritma MEMBUAT_MINUMAN_KOPI
1. Masukkan satu sendok makan gula ke dalam cangkir.
2. Masukkan satu sendok teh kopi ke dalam cangkir.
3. Tuangkan air panas ke dalam cangkir hingga penuh.
4. Aduk isi cangkir selama 30 detik.

Algoritma MEMBUAT_MINUMAN_KOPI
1. Masukkan satu sendok teh kopi ke dalam cangkir.
2. Tuangkan air panas ke dalam cangkir hingga penuh.
3. Aduk isi cangkir selama 30 detik.
4. Jika ingin berasa manis maka
Masukkan satu sendok makan gula ke dalam cangkir kemudian aduk isi cangkir
selama 30 detik.

Dari dua contoh di atas, tampak bahwa untuk menyelesaikan suatu masalah yang sama,
masing-masing orang dapat membuat algoritma yang berbeda.

Algoritma MENGIRIM_SURAT
{Ada dua orang A dan B. A ingin mengirimkan surat yang berisi informasi bahwa A
berhasil lulus UMPTN dan A ingin memastikan bahwa informasi tersebut sampai ke B}
1. Tulis surat kemudian kirimkan ke B.
2. Mulai menghitung waktu tunggu dari 0.
3. Cek datangnya surat balasan, jika sudah datang maka
Cek apakah di dalam surat balasan terdapat informasi bahwa B sudah menerima
surat A. Jika ada maka stop
4. Cek lamanya waktu tunggu, jika sudah 1 minggu maka lanjutkan ke langkah 1.
5. Lanjutkan ke langkah 3.

Moh Edi Wibowo( [1] Munir, R., “Algoritma dan Pemrograman dalam Bahasa Pascal dan C”, Informatika
Bandung, 2000)