A. VARIABEL, KONSTANTA
DAN TIPE DATA
Variabel, konstanta dan tipe data
merupakan tiga hal yang akan selalu kita jumpai ketika kita membuat program.
Bahasa pemrograman apapun dari yang paling sederhana sampai yang paling kompleks,
mengharuskan kita untuk mengerti ketiga hal tersebut.
1. Variabel
Variabel adalah tempat dimana kita dapat
mengisi atau mengosongkan nilainya dan memanggil kembali apabila dibutuhkan.
Setiap variabel akan mempunyai nama (identifier) dan nilai. Perhatikan contoh
berikut.
Contoh Nama variabel dan nilai.
username = “joni”
Nama = “Al-Khawarizmi”
Harga = 2500
HargaTotal = 34000
Pada contoh di atas, username, Nama,
harga dan HargaTotal adalah nama dari variabel sedangkan “joni”, “Al-Khawarizmi”,
2500 dan 34000 adalah nilai dari masing-masing variabel. Nilai-nilai ini akan tersimpan
di dalam nama variabel masing-masing sepanjang tidak kita rubah. Pada sebagian
besar bahasa pemrograman, variabel harus dideklarasikan lebih dulu untuk
mempermudah compiler bekerja. Apabila variabel tidak dideklarasikan maka setiap
kali compiler bertemu dengan variabel baru pada kode program akan terjadi waktu
tunda karena compiler harus membuat variable baru. Hal ini memperlambat proses
kerja compiler. Bahkan pada beberapa bahasa pemrograman, compiler akan menolak
untuk melanjutkan proses kompilasi. Pemberian nama variabel harus mengikuti
aturan yang ditetapkan oleh bahasa pemrograman yang kita gunakan. Namun secara
umum ada aturan yg berlaku untuk hampir semua bahasa pemrograman. Aturan-aturan
tersebut yaitu:
• Nama variabel harus diawali dengan huruf.
• Tidak boleh menggunakan spasi pada satu nama variabel. Spasi biasa diganti
dengan karakter underscore (_).
• Nama variabel tidak boleh mengandung karakter-karakter khusus, seperti : .,+,
-, *, /, <, >, &, (, ) dan lain-lain.
• Nama variabel tidak boleh menggunakan kata-kata kunci di bahasa pemrograman
2. Konstanta
Konstanta adalah variabel yang nilai datanya bersifat tetap dan tidak biasa diubah.
Jadi konstanta adalah juga variabel bedanya adalah pada nilai yang disimpannya.
Jika nilai datanya sepanjang program berjalan tidak berubah-ubah, maka sebuah
varibel lebih baik diperlakukan sebagai konstanta. Pada sebuah kode program,
biasanya nilai data dari konstanta diberikan langsung di bagian deklarasi
konstanta. Sedangkan untuk variabel biasanya hanya ditentukan nama variabel dan
tipe datanya tanpa isian nilai data. Aturan penamaan variabel juga berlaku
untuk penamaan konstanta. Demikian juga aturan penetapan tipe data. Sebagai
contoh, jika kita membuat program perhitungan matematik yang menggunakan nilai
pi (3.14159) yang mungkin akan muncul dibanyak tempat pada kode program, kita
dapat membuat pi sebagai konstanta. Penggunaan konstanta pi akan lebih memudahkan
penulisan kode program dibanding harus mengetikkan nilai 3.14159 berulang-ulang.
3. Tipe Data
Tipe data adalah jenis data yang dapat
diolah oleh komputer untuk memenuhi kebutuhan dalam pemrograman komputer.
Setiap variabel atau konstanta yang ada dalam kode program, sebaiknya kita
tentukan dengan pasti
tipe datanya. Ketepatan pemilihan tipe data pada variabel atau konstanta akan sangat
menentukan pemakaian sumberdaya komputer (terutama memori komputer). Salah satu
tugas penting seorang programmer adalah memilih tipe data yang sesuai untuk
menghasilkan program yang efisien dan berkinerja tinggi. Ada banyak tipe data yang tersedia tergantung
jenis bahasa pemrograman yang dipakai. Namun secara umum dapat di kelompokkan
seperti dibawah ini :
1. Tipe data primitive adalah tipe data dasar yang tersedia secara langsung pada
suatu bahasa pemrograman.
2.
Tipe data composite adalah tipe data bentukan yang terdiri dari dua atau lebih
tipe data primitive.
3. Tipe data numeric digunakan pada variabel atau konstanta untuk menyimpan
nilai dalam bentuk bilangan atau angka. Semua bahasa pemrograman menyediakan
tipe data numeric, hanya berbeda dalam jenis numeric yang diakomodasi. Jenis
yang termasuk dalam tipe data numeric antara lain integer (bilangan bulat), dan
float (bilangan pecahan). Selain jenis, dalam bahasa pemrograman juga
diterapkan presisi angka yang digunakan, misalnya tipe data Single adalah tipe
data untuk bilangan pecahan dengan presisi yang terbatas, sedangkan tipe data
Double adalah tipe data untuk bilangan pecahan dengan presisi yang lebih akurat.
Pada bab-bab berikutnya yang membahas aplikasi bahasa pemrograman bagian ini
akan diuraikan lebih lanjut.
Pengurutan Data pada Array
Permasalahan
lain dalam array yang juga banyak digunakan adalah bagaimana mengurutkan
elemen-elemen dari variabel array tersebut.
Ada beberapa algoritma yang dapat digunakan untuk mengurutkan sekumpulan
bilangan, antara lain bubble sort, selection sort, shell sort, quick sort, dan
lain-lain. Pada buku ini kita akan membahas satu algoritma yaitu bubble sort.
Meskipun kinerjanya tidak sebaik algoritma yang lain, algoritma ini
mudah dimengerti dan banyak digunakan.
B. OPERASI FILE
File seringkali digunakan untuk
menyimpan data agar data tidak hilang. Data atau yang ada dan dihasilkan pada
program akan hilang ketika program diakhiri, sehingga file digunakan untuk
menyimpan data tersebut. Ada dua jenis file yaitu file program dan file data.
File program berisi kode-kode program sedangkan file data hanya berisi data.
File data terdiri dari dua jenis yaitu file
data berurutan (sequential data file) dan file data acak (random-access data
file).
Perbedaan utama dari kedua jenis file data ini adalah dapat dilihat pada table
berikut.
File data berurutan
|
File data acak
|
-
Record atau baris data harus dibaca berurutan mulai dari yang pertama
|
-
Record tidak perlu dibaca berurutan
|
-
Panjang field untuk setiap record tidak perlu sama
|
-
Panjang field untuk setiap record harus sama
|
-
Pengubahan serta penambahan record tertentu sukar dilakukan
|
-
Pengubahan serta penambahan record lebih mudah dilakukan
|
-Algoritma Penulisan Data pada File
Algoritma yang digunakan untuk
penulisan data untuk file data berurutan maupun acak secara prinsip sama, hanya
modusnya yang berbeda. Berikut ini adalah algoritma penulisan data dalam SE.
Open “modus”, , “nama file data”
Write , field 1, field 2, .. field n
Close buffer number
Modus O menunjukkan file ini dibuka untuk ditulisi.
Contoh
penerapan algoritma penulisan data.
Misalkan kita punya file data dengan nama “siswa.dat” yang field-nya adalah nama
siswa, alamat, nomor telepon. Maka untuk menuliskan data adalah sebagai
berikut.
Open “O”, #1, “siswa.dat”
Write #1, , ,
Close #1
Notasi #1 menunjukkan siswa.dat akan ditempatkan dalam buffer no 1. Notasi ini
harus sama digunakan di seluruh progam di atas. Artinya kalau kita menempatkan
suatu file dengan nomor buffer #1 maka ketika membuka, menulis, membaca dan
menutup harus menggunakan notasi tersebut. Demikian juga bila kita menempatkan
pada buffer no #2.
-Algoritma
Pembacaan Data pada File
Algoritma membaca data algoritmanya
hampir sama dengan menuliskan data, tetapi modus yang digunakan tidak O tetapi
I. I adalah input yang berarti file data dibuka untuk dibaca datanya sebagai input.
Berikut ini algoritmanya dalam SE.
Open “modus”, , “nama file data”
While not EOF:
Input , field 1, field 2, .. field n
Print field 1, field 2, .. field n
End while
Close buffer number
Pernyataan While Not EOF digunakan untuk
memeriksa apakah sudah ada pada baris terakhir dari data. Jika belum maka baris-baris
data akan dibaca dan dicetak sampai baris terakhir. Pernyataan input digunakan
untuk mengambil data dari file untuk dimuat ke dalam program. Sedangkan
pernyataan print digunakan untuk mencetak data ke layar komputer.
Contoh
penerapan algoritma pembacaan data.
File data dengan nama “siswa.dat” seperti pada contoh diatas yang field-nya adalah
nama siswa, alamat, nomor telepon. Maka untuk membaca data adalah sebagai
berikut.
Open “I”, #2, “siswa.dat”
While not EOF:
Input #2, , ,
Print , ,
End while
Close buffer number
C. RINGKASAN
• Variabel adalah tempat dimana kita dapat mengisi atau mengosongkan nilainya
dan memanggil kembali apabila dibutuhkan. Setiap variabel akan mempunyai nama
(identifier) dan nilai.
• Konstanta adalah variabel yang nilai datanya bersifat tetap dan tidak bisa
diubah.
• Tipe data adalah jenis data yang dapat diolah oleh komputer untuk memenuhi
kebutuhan dalam pemrograman komputer.
• Tipe data dapat dikelompokkan menjadi tipe data primitive dan tipe data
composite. Tipe data primitive terdiri dari numeric, character, dan bolean.
Sedangkan tipe data composite terdiri dari array, record/struct, image, date
time, subrange, enumerasi, obyek dan variant.
• Algoritma adalah urutan langkah-langkah logis penyelesaian masalah yang
disusun secara sistematis. Algoritma harus benar dan harus berhenti. Setelah
berhenti, algoritma memberikan hasil yang benar.
• Algoritma dapat ditulis dengan cara Structured English, Pseudocode dan
Flowchart.
• Struktur berurutan terdiri satu atau lebih instruksi. Tiap instruksi dikerjakan
secara berurutan sesuai dengan urutan penulisannya.
• Pada struktur percabangan, program akan berpindah urutan pelaksanaan jika
suatu kondisi yang disyaratkan dipenuhi.
• Struktur pengulangan terdiri dari dari kondisi pengulangan dan badan
pengulangan dan dapat dilakukan dengan For dan While.
• Array adalah struktur data yang menyimpan sekumpulan elemen yang bertipe
sama, setiap elemen diakses langsung melalui indeksnya. Operasi pencarian pada
array dapat dilakukan dengan cara linear search sedangkan pengurutan dengan
metode bubblesort.
• File data ada yang bersifat urut dan ada yang acak. Metode pembacaan dan
penulisan dibedakan dari modusnya.