I.
Program Quick Basic Dari Flowchart
Sebuah bank mengadakan seleksi untuk penerimaan calon pegawai baru.
Calon yang akan diterima adalah yang memenuhi kriteria tersebut:
a. Untuk pria tinggi badan minimal 165 cm, dan umur minimal 18 tahun.
b. Untuk wanita tinggi badan minimal 160 cm dan umur minimal 17
tahun.
c. IPK minimal 3.0
Tuliskan program flowchart dan quick BASIC untuk permasalahan di atas. Data
masukan adalah nama calon, jenis kelamin, tinggi badan, umur dan IPK. Keluaran
yang diinginkan adalah:
Nomor Nama Keterangan
1
2
...
Total Diterima = .... orang
Total Ditolak = .... orang
Catatan : Kolom keterangan diisi dengan “diterima” jika semua kriteria
terpenuhi dan “ditolak” untuk selain itu.
Jawaban:
Flowchart
START
TBMP=165, UMP=18,
TBMW=160, UMW=17,
IPKM=3.0
PRINT HEADER
READ NC$ ,J$ ,TB,
U,IPK
A
A
NC$=” PRINT
STOP
X” TD,TDT
IPK>=
IPKM
C
J$=”PRI TB>= UM>
TBM D
A” =
W
UMW
TB>TBM
P
UM>=UMP
C
TDT=TDT+1
TD=TD+1
KET$=”DI TOLAK”
KET$=”DI TERIMA”
NO=NO+1
PRINT NO,NC$,KET$,
TD,TDT
B
2
QUICK BASIC
CLS
TBMP = 165
UMP = 18
TBMW = 160
UMW = 17
IPKM = 3
PRINT
PRINT STRING$(80, "-")
PRINT "NO."; TAB(15); "NAMA"; TAB(35); "KETERANGAN"
PRINT STRING$(80, "-")
BACA:
READ NC$, J$, TB, U, IPK
IF NC$ = "X" THEN
PRINT STRING$(80, "-")
PRINT TAB(15); "TOTAL YANG DITERIMA = "; TDT; " ORANG"
PRINT TAB(15); "TOTAL YANG DITOLAK = "; TD; " ORANG"
PRINT STRING$(80, "-")
END
END IF
NO = NO + 1
3
IF IPK >= IPKM AND J$ = “PRIA” AND TB >= TBMP AND UM >= UMP
THEN
KET$ = "DI TERIMA" : TG = TG + 1: GOTO CETAK
ELSE
IF IPK >= IPKM AND J$ = “WANITA” AND TB >= TBMW AND UM
>= UMW THEN
KET$ = “DI TERIMA” : TG = TG +1: GOTO CETAK
ELSE
KET$ = “DI TOLAK” : TL =TL + 1: GOTO CETAK
END IF
CETAK:
PRINT NO; TAB(15); NC$; TAB(35); KET$
GOTO BACA:
DATA HENNY,WANITA,162,17,2,MARCELUS,PRIA,153,19,3
DATA ANDREAS,PRIA,175,29,3,ELISA,WANITA,160,35,3
DATA JENNY,WANITA,170,15,2,ALVIN,PRIA,175,30,3
DATA MARIO,PRIA,165,12,3,WINDY,WANITA,168,21,3.5
DATA SISCA,WANITA,150,40,2.3,AARON,PRIA,175,25,4
DATA X,W,0,0,0
END
4
II. Perbaikan Program Quick Basic
Berikut ini akan saya tampilkan 2 macam program quick basic yang
salah, dimana kedua program tersebut harus kita perbaiki, yaitu :
Soal 1 :
TOTAL =0
FOR K = 1 TO 5
READ JUMLAH_BARANG,HARGA_SATUAN
HARGA= JUMLAH_BARANG*HARGA_SATUAN
TOTAL = TOTAL + HARGA
NEXT K
IF TOTAL >= 3000000 THEN
BONUS = “PAYUNG”
ELSE
BONUS = “KALENDER”
ENDIF
PRINT “ TOTAL BELANJA = RP. “, TOTAL
PRINT “ SELAMAT ANDA MENDAPAT HADIAH : “, BONUS
END
DATA 10, 100000
DATA 20, 20000
DATA 5, 150000
DATA 1, 750000
5
DATA 3, 500000
Perbaikan
Soal 1 :
CLS
FOR K = 1 TO 5 STEP
READ JUMLAH_BARANG, HARGA_SATUAN
HARGA = JUMLAH_BARANG*HARGA_SATUAN
TOTAL = TOTAL + HARGA
IF TOTAL >= 3000000 THEN
BONUS$ = “PAYUNG”
ELSE
BONUS$ = “KALENDER”
END IF
PRINT “TOTAL BELANJA =RP. “, TOTAL
PRINT “SELAMAT ANDA MENDAPAT HADIAH :“, BONUS
NEXT K
END
DATA 10, 100000
DATA 20, 20000
DATA 5, 150000
DATA 1, 750000
DATA 3, 500000
Data input
6
Data output
7
Soal 2 :
CLS
FOR I = 1 TO 5
R3AD NAMA, JAM_KERJA
IF JAM_KERJA > 22 THEN
BONUS$ = 100000
ELSE
BONUS$ = 0
END IF
GAJI = JAM_KERJA*20000 + BONUS$
PRINT NAMA, JAMA_KERJA,GAJI
NEXT K
END
DATA LALA, 20, LULU, 22, LOLO, 18
Perbaikan
Soal 2 :
CLS
FOR I = 1 TO 3
READ NAMA$, JAMKERJA
IF JAMKERJA > 20 THEN
BONUS = 10000
ELSE
8
BONUS = 0
END IF
GAJI = <JAMKERJA * 20000> + BONUS
PRINT NAMA$, JAMKERJA, GAJI
NEXT I
END
DATA LALA, 20, LULU, 22, LOLO, 18
Data Input
Data output
9
III. PRINT, LPRINT, PRINT TAB, PRINT SPC,
PRINT USING, IF THEN ELSE
III.1. The PRINT statement - Writing data to the screen
The print statement is used to write data on the screen.
You can write:
Numbers
Characters, Sentences, Texts
The contents of memory variables and constants
Note: Characters and sentences are written within inverted commas(" "). What
is written within inverted commas is displayed as is during run time.
The following examples illustrate the different ways to use the PRINT
statement:
Code Screen Output Comment
PRINT "5" 5 Writing character 5 to the
screen.
PRINT 5 5 Writing number 5 to the
screen.
PRINT 5 5 Note the space before the
PRINT "5" 5 number 5 in the first line.
BASIC always puts a space
for +/- signs when printing a
number.
PRINT "5 + 7" 5+7 What is within inverted
10
commas is written as is.
PRINT 5 + 7 12 Writing the sum of 5 and 7 to
the screen
PRINT 6 * 7 42 The * sign is used for
multiplication.
PRINT 12 / 4 3 The / sign is used for division.
PRINT 8 - 3 5
a=3 3 a, b, c and d are memory
b=4 4 variables.
c=a+b 7 The value 3 is assigned to
d=a*b 12 memory variable a.
PRINT a The value 4 is assigned to
PRINT b memory variable b.
PRINT c In the 3rd line, the computer
PRINT d looks assigns the sum of
memory variables a and b to
memory variable c.
In the 4th line the computer
looks assigns the product of
memory variables a and b to
memory variable d.
In line 5, the computer looks
for the value of memory
variable a and writes it to the
screen.
a=3 a Did you expect to get 3 on the
PRINT "a" screen? Don't forget that
whatever is within inverted
commas is written as is.
11
The correct code should be:
a=3
PRINT a
PRINT prints things to the screen. Don't forget to put what you want to say in
double-quotation marks (").
Press any key to continue?
At the bottom of the screen you will see a message "Press any key to
continue". Press <Enter> to get back to QBASIC. This is QBASIC's way of
giving you enough time to see what your program did.
As a cool shortcut, use ? for PRINT. Try it. Press <Enter> to start typing on a
new line. Now type this:
? "Programming is fun."
and press <Enter> again.
Isn't that nice? The ? becomes the word PRINT. That should save us a lot of
typing in the long run.
You can try running the program again (remember? Click on Run, then Start
on the menu). You should see something like this:
C:\WINDOWS> qbasic
Welcome to QBASIC. Your wish is my command.
Welcome to QBASIC. Your wish is my command.
Programming is fun.
PRINT Statement
Tujuan : Untuk menampilkan data berupa teks atau angka pada layar monitor
Bentuk : PRINT [list of expressions][;]
12
? [list of expressions][;]
Ket : List of expressions dapat diisi berupa numeric(angka) atau karakter
(huruf) sesuai yang diinginkan
Contoh:X=5Print x+5, x-5,x*(-5)
End
Hasilnya: 10 0-25
Cls
Locate 5, 15
Print “BELAJAR QUICK BASIC”
End
Hasilnya : BELAJAR QUICK BASIC
For x = 1 to 5
J=j+5
K=k+10
? j;k;
next x
Hasilnya: 5 10 10 20 15 30 20 40 25 50
13
III.2. PRINT TAB(X)
Digunakan untuk mengatur jarak ekspresi ke ekspresi berikutnya pada satu
baris pencetakan atau memungkinkan kita mengatur mulai kolom keberapa
hasil dicetak.
B. U. PRINT TAB (n) [{;II,} TAB(m) I ekspresi [{;II,} …]]
Contoh :
PRINT TAB(5) ; “BELAJAR” Output dari program tsb :
PRINT TAB(3) ; “ BAHASA” 01234567
PRINT “ BASIC” BELAJAR
END BAHASA
BASIC
III.3. PRINT SPC(X)
In Microsoft QuickBasic Versions 4.00, 4.00b, and 4.50, Microsoft Basic
Compiler Versions 6.00 and 6.00b, and Microsoft Basic PDS Version
7.00, the SPC() function does not cause output to wrap around to the next
line(s). In QuickBasic Version 3.00, the SPC() function wraps to the next
line.
For comparison, the SPACE$() function wraps to the next line in all
versions.
The nonwrapping behavior of the SPC() function is an intentional feature
in QuickBasic Versions 4.00, 4.00b, and 4.50, Basic compiler Versions
6.00 and 6.00b, and Microsoft Basic PDS Version 7.00. If the value given
to the SPC() function is greater than the defined screen width, the spacing
14
will be: <desired spacing> MOD <width>. For example, the following line
would actually print "Hello" in column 3 of the same line:
PRINT SPC(83);"Hello"
The documentation on this function is incomplete and will be corrected.
Microsoft will post new information here as it becomes available.
In the following code example, both outputs will incorrectly be on the
same line:
REM *** SPC() problem example ***
CLS : LOCATE 1,1 : PRINT " XXX"
LOCATE 1,1 : PRINT SPC(80); "Hello";
END
The SPC(80) function should force the "Hello" literal to be displayed on
the second line. The SPACE$() function can be used to simulate the line
wrap, when given the space (Hex 20) character. Note that the SPACE$()
function overwrites any screen information that comes in its path with the
new SPACE$() character.
III.4. PRINT USING
english
The PRINT USING statement is not designed to print the values of an
expression list in the 14-character print zones. Instead, it ignores the
comma and treats it like a semicolon. In QuickBasic version 4.50, the
syntax checker correctly changes the comma to a semicolon. However,
several manuals (listed below) and the online help system incorrectly state
that the comma is syntactically legal.
To work around this restriction of PRINT USING and to allow printing at
the next print zone (at every 14th character), you can do the following: end
the PRINT USING statement with a semicolon, then follow with a PRINT
15
statement that just has a comma as an argument, and then PRINT the next
item desired to be in the next print zone. For example:
x = 33.45
PRINT USING "###.##"; x; ' Prints x, keeping cursor on same line.
PRINT , ' Moves cursor to next print zone on same line.
PRINT x ' x prints on same line at column 14, the next print zone.
Documentation Errors
The incorrect syntax for the PRINT USING statement is as follows:
PRINT USING formatstring; expressionlist [{,|;}]
The incorrect definition for the PRINT USING statement is as follows:
The position of each printed item is determined by the punctuation used to
separate the items in the list. Basic divides the line into print zones of 14
spaces each. In the expression list, a comma makes the next value print at
the start of the next zone. A semicolon makes the next value print
immediately after the last value.
This incorrect syntax or definition is given in each of the following
references:
1. Page 275 of the "Microsoft Basic 7.0: Basic Language Reference" for
Microsoft Basic PDS versions 7.00 and 7.10
2. Page 335 of the "Microsoft QuickBasic 4.0: Basic Language
Reference" for QuickBasic 4.00 and 4.00b
3. Page 287 of the "Microsoft QuickBasic: Basic Language Reference"
for QuickBasic 4.50
4. Page 335 of the "Microsoft Basic Compiler 6.0: Basic Language
Reference" for Microsoft Basic Compiler versions 6.00 and 6.00b
16
5. The online help system for Microsoft QuickBasic 4.50 under the entry
for PRINT USING statement
6. The online help system for Basic PDS 7.00 under the entry for the
PRINT USING statement
The following is a code example that demonstrates that PRINT USING
treats commas as if they were semicolons. When run in the [Link] or
[Link] environment, the code example will correctly and automatically
substitute a semicolon between the variables "a" and "b" for the comma:
a=3.45
b=5.23
PRINT USING "##.##";a,b
The output is as follows:
3.45 5.23
Indonesia
Digunakan untuk menampilkan informasi yang berupa nilai variabel
dengan format yang rapi, biasanya digunakan dalam pencetakan tabel-
tabel.
B. U. PRINT USING Format Tampilan; Deret Ekspresi
Ket : - Format menunjukkan format pencetakan yang berupa
ekspresi
string yang berisi sejumlah karakter
- Deret Ekspresi adalah ungkapan yang nilainya akan
dicetak
Tabel parameter Format dalam PRINT USING
Forma Digunakan untuk:
17
t
# Menampilkan nilai numerik biasa dengan digit sebanyak tanda
(#)
\ \ Menampilkan nilai string sejumlah spasi diantara tanda backslash
^^^^ Mencetak bilangan riil presisi tunggal dan atau presisi ganda
menggunakan format saintifik
+# Menampilkan tanda (+) pada nilai positif untuk nilai numerik
-# Menampilkan tanda (-) pada nilai negatif pada sebuah nilai
numerik
$# Menampilkan tanda ($) pada awal nilai numerik
**# Menampilkan tanda (*) pada sisa spasi yang kosong diawal nilai
numerik
#, Menentukan bahwa pemisah untuk digit ribuan adalah tanda
koma
#.# Menentukan tanda pecahan desimal
! Hanya menampilkan karakter pertama dari string
& Menampilkan seluruh karakter dari string
_ Menampilkan karakter dibelakang tanda garis bawah tersebut
sebagai karakter biasa
Contoh : NAMA$ = “Maria Herjani”
N = 95.65
PRINT USING “ NAMA : \ \ NILAI ###.## “;
NAMA$,N
Ket : \ \ 7 karakter diisi dengan 7 huruf sesuai dengan spasi tanda
tsb.
Output : Maria H NILAI 95.65
18
III.5. LPRINT
Syntax:
LPRINT [expressionlist][;|,]
Purpose:
To send output to the printer located on LPT1.
LPRINT functions the same way as PRINT, except the output is sent to a
printer instead of the screen. LPRINT will accept the same parameters that
PRINT will, such as variables (numbers and string expressions), static text,
and multiple combinations of each:
LPRINT "Welcome to the QBasic Station"
LPRINT a$
LPRINT a$ + b$
LPRINT a + b
LPRINT "A + B = "; a + b
LPRINT "Welcome to the QBasic Station, "; name$
LPRINT "I am "; name$; " whether you like it or not!"
LPRINT "Item #", num, " will cost $"; price
A comma, when used to tie expressions together, will cause the expression
after the comma to be printed at the next available tab stop. Using the
semicolon instead of the comma will cause the next expression to be printed
immediately after the previous. Notice in the above exampes that whitespace
is inserted to result in readable spacing.
Since LPRINT will produce relatively identical output on the printer as
PRINT will on the monitor, the PRINT command can be substituted for
LPRINT if any adjustments need to be made to what is being printed. It saves
time and paper!
19
LPRINT defaults to a column width of 80 characters. The WIDTH statement
can be used to change this default.
III.6. IF...THEN
English
Let's make that last program a little smarter. I want to be able to identify
intruders playing with my programs. Wouldn't it be great if the computer
could recognize someone's name and print a special message for them? How
about this:
CLS
INPUT "Enter your name: ", Name$
IF Name$="Mike" THEN
PRINT "Go Away!"
ELSE
PRINT "Hello, "; Name$; ". How are you today?"
END IF
You can change the Name$="Mike" to Name$="Joe", or whoever you want to
go away (like a brother or sister). Run the program and type in your name
(hopefully it isn't Mike). You should see the same old message as before. Now
run it again and try entering "Mike" (or "Joe" or whatever you changed it to).
"Mike" Is Not The Same As "mike"
If it didn't tell the right person to go away, make sure you typed the name
correctly. In QBASIC, "Mike" is not the same as "mike", so if you don't type
the first letter in upper-case, the program won't work. Make sure you enter the
name exactly the way you put it in the program.
IF...THEN
The "IF..THEN..ELSE..END IF" statement in this program checks to see if
Name$ has "Mike" in it. If so, then it does the PRINT statement after the
20
"THEN". If Name$ isn't "Mike", it does the PRINT statement after the
"ELSE". "END IF" tells QBASIC that the "IF" is over.
Conditions
The Name$="Mike" portion of the IF...THEN is called the "condition". With
numbers you can also check for "greater than" and "less than":
CLS
INPUT "Enter a number: ", Number
IF Number < 100 THEN
PRINT "Your number was less than 100"
ELSE
PRINT "Your number was greater than or equal to 100"
END IF
If you want to check for "greater than", use "Number > 100". Equals works
just like before, "Number = 100". Another option is "not equal" which can be
done like this: "Number <> 100".
IF...THEN is one of the most powerful features of QBASIC. Using IF...THEN
can make your programs very interesting. It lets your program make decisions
and do something appropriate.
Indonesia
Statement IF-THEN digunakan untuk memeriksa sebuah kondisi dan
mengeksekusi satu atau lebih baris program, jika dan hanya jika kondisi
terpenuhi.
Instruksi IF-THEN dengan syarat tunggal, merupakan instruksi untuk
memeriksa sebuah kondisi saja.
Instruksi IF-THEN dengan syarat majemuk, merupakan sebuah instruksi
untuk memeriksa lebih dari satu buah kondisi. Kondisi-kondisi tersebut
dihubungkan dengan operator-operator logika, seperti AND atau OR.
21
Statement IF-THEN juga digunakan untuk memeriksa kondisi dari data
string. Apabila kondisinya terpenuhi maka baris-baris program akan
[Link] pemeriksaan kondisi data string digunakan operator
relasi yang akan mendeteksi nilai ASCII dari data tersebut.
Dengan IF...THEN...ELSE, pemrograman dapat mengatur tindakan yang
akan dilakukan kalau kondisi bernilai benar ataupun tindakan yang akan
dijalankan kalau kondisi salah.
Bentuk umum IF...THEN...ELSE dibagi menjadi 2 kelompok :
1. IF...THEN...ELSE Satu baris
2. IF...THEN...ELSE Banyak baris
IF...THEN...ELSE Satu Baris
Di dalam program jika kita menjumpai dua kemungkinan atau pilihan
pencabangan, kita bisa menggunakan IF...THEN...ELSE satu baris.
Bentuk Umum :
IF kondisi THEN { statement1 │nobar1 │GOTO label1 }
[ ELSE { statement2 │ nobar2 │ GOTO label2 } ]
dengan parameter-parameternya :
Kondisi : syarat yang akan ditest
statement1,
statement2 : statement yang akan dikerjakan
nobar1, nobar2 : nomor baris yang dituju
22
label1, label2 : label baris yang dituju
Parameter :
kondisi atau syarat yang ditest dinyatakan dengan operator relasi atau
operator pembanding (<, <=, =, >=, >, < >).
Nobar1, dan nobar2 menunjukkan label baris yang berupa angka
Label1, dan label2 menunjukkan label baris yang berupa label
alphanumeris (diawali dengan huruf)
Bentuk umum diatas bisa dijelaskan sebagai berikut :
Jika kondisi bernilai benar, maka salah satu dari tiga pilihan dibelakang
statement THEN akan dikerjakan.
Jika kondisi salah, maka salah satu dari tiga pilihan dibelakang statement
ELSE akan dikejakan.
Jika statement ELSE tidak ditulis maka proses eksekusi langsung akan
melompat ke baris dibawah statement IF.
Bagan alir IF satu baris
23
Menghitung Menghitung
nilai kondisi
nilai kondisi
inilai kondisi
Tidak Kondisi Ya Tidak Kondisi Ya
Dipenuhi Dipenuhi
Kerjakan Kerjakan Kerjakan
? ?
statement Statemen2 Statement1
Ke statement berikutnya Ke statement berikutnya
a b
Gambar a. Tanpa statement ELSE
b. Dengan statement ELSE
Contoh :
1. IF i >= kali THEN 20
2. IF ( i <= 100 ) AND ( i >= 80 ) THEN PRINT i
3. IF ( i >= 100 ) OR ( i <= 80 ) THEN PRINT i
4. IF i >= kali THEN GOTO 20 ELSE kali = kali + 1
5. CLS
INPUT “Total Pembelian :” , TotalBeli
Korting = 0
IF TotalBeli >= 100000 THEN Korting = .1 * TotalBeli ElSE Korting = 0
PRINT “Korting = “ ; Korting
END
Dalam Penulisan statement IF...THEN...ELSE satu baris, semua
parameter harus ditulis menjadi satu baris statement.
24
IF...THEN...ELSE Banyak baris
Di dalam program jika kita menjumpai lebih dari dua kemungkinan atau
lebih pilihan pencabangan, kita bisa menggunakan IF...THEN...ELSE banyak
baris.
4 aturan penggunaan blok IF...THEN...ELSE yaitu :
1. Di belakang statement THEN tidak boleh ada statement apapun selain
baris komentar. Jika anda menuliskan sesuatu statement, kompiler akan
menganggapnya sebagai statement IF...THEN...ELSE satu baris.
2. Kata ELSE, ELSEIF dan END IF hanya boleh diawali dengan nomor
baris atau label baris. Jika tidak, maka kata ini harus merupakan kata
awal dari baris tersebut.
3. Blok IF harus terletak sebagai statement pertama dalam suatu baris.
4. Blok harus diakhiri dengan END IF.
Bentuk Umum :
IF kondisi1 THEN
Statement1
[ ELSEIF kondisi2 THEN
[ statement2 ] ]
[ ELSE
[ statementn ] ]
25
END IF
dengan parameter-parameternya :
kondisi1,
kondisi2, … : syarat yang harus ditest
statement1,
statement2, … : blok statement yang akan dikerjakan sesuai
dengan kondisi yang dipenuhi.
Bentuk umum diatas bisa dijelaskan sebagai berikut :
Jika kondisi1 bernilai benar, blok statement1 akan dikerjakan diteruskan ke
statement IF.
Jika kondisi1 bernilai salah, kompiler akan mentest [Link] bernilai
benar, maka blok statement2 akan dikerjakan, diteruskan ke statement
END IF. Dst
Contoh :
CLS
PRINT “1. Nasi Soto Ayam”
PRINT “2. Nasi Rames”
INPUT “Pilihan (1..2) : “, Pil%
IF PIL% = 1 THEN
PRINT “Nasi Soto Ayam”
ELSEIF PIL% = 2 THEN
PRINT “Nasi Rames”
ELSE
26
PRINT “Pilihan Anda tidak dimengerti”
END IF
END
27
BIBLIOGRAPHY
[Link]
dengan-QBasic on May, 5 2010 21:34
[Link] on May, 5 2010 21:45
[Link]
nt+I-O&Percabangan(Minggu+Ke+III).doc on May, 6 2010 21:09
[Link] on May, 6 2010 21:12
[Link]
ntKendali(Minggu+ke+VII).doc on May, 6 2010 21:32
[Link] on May, 7
2010 22:55
[Link]
[Link] on May, 7 2010 23:03
[Link] on
May, 8 2010 09:12
[Link]
on May, 8 2010 09:20
[Link] 9.36 on May, 8 2010
09:24
28