Anda di halaman 1dari 4

Struktur Percabangan Do case...

endcase
Di dalam bahasa pemrograman istilah percabangan / kondisi tidak asing lagi, hal ini dikarenakan
pada suatu saat ada kondisi yang harus dipilih sesuai dengan nilai yang diberikan, pada
pemrograman database menggunakan visual foxpro, terdapat suatu struktur percabangan / kondisi
Do case...endcase, sintaknya
DO CASE
CASE lExpression1
[Commands]
[CASE lExpression2
[Commands]]
...
[CASE lExpressionN
[Commands]]
[OTHERWISE
[Commands]]
ENDCASE

Dimana :
lExpression1 berisi nilai yang ditentukan
Commands perintah yang diberikan jika ekspresi terpenuhi
OTHERWISE akan dikerjakan bila ekspresi 1 s/d n tidak terpenuhi

Sebagai contoh buatlah desain program dengan tampilan berikut:


Contoh program do case untuk tipe data bilangan

Pengaturan dari tiap obyek diatur pada tabel berikut :


Nama Obyek Properties Nilai
Form1 Caption Struktur docase endcase
Text1 Value =0
Text2 Value =0
Text3 Value =0
Command1 Caption Proses 1
Command2 Caption Proses 2
Command3 Caption Proses 3
Sedangkan untuk listing programnya :
Nama Obyek Procedure
Command1 Click
do case
case thisform.text1.value >= 60
thisform.label2.caption="Lulus "
otherwise
thisform.label2.caption="Gagal "
endcase
Command2 Click
with thisform
do case
case .text2.value>=80
.label9.caption="Nilai A"
case .text2.value>=70
.label9.caption="Nilai B"
case .text2.value>=60
.label9.caption="Nilai C"
case .text2.value>=41
.label9.caption="Nilai D"
otherwise
.label9.caption="Nilai E"
endcase
endwith
Command3 Click
with thisform
do case
case .text3.value >=0 and .text3.value<=5
.label11.caption="Balita"
case .text3.value >=6 and .text3.value<=14
.label11.caption="Anak-anak"
case .text3.value >=15 and .text3.value<=23
.label11.caption="A B G"
case .text3.value >=24 and .text3.value<=35
.label11.caption="Dewasa"
case .text3.value >=36 and .text3.value<=60
.label11.caption="Orang Tua"
otherwise
.label11.caption="Manula"
endcase
endwith

Struktur Percabangan If..Else..Endif


Hampir sama fungsi If..Else..Endif dengan Do case.. Endcase, berfungsi untuk memberikan
perintah dalam suatu kondisi tertentu. Secara umum sintak dari If..Else..Endif adalah sebagai
berikut:
IF lExpression [THEN]
Commands
[ELSE
Commands]
ENDIF
Dimana :
lExpression berisi nilai yang diberikan untuk kondisi tertentu
Commands berisi perintah yang diberikan jika kondisi terpenuhi

Penggunaan percabangan If..Else..Endif dengan tipe data angka


Pengaturan dari tiap obyek diatur pada tabel berikut :
Nama Obyek Properties Nilai
Form1 Caption Struktur If..Else..Endif
Text1 Value =0
Text2 Value =0
Text3 Value =0
Command1 Caption Proses 1
Command2 Caption Proses 2
Command3 Caption Proses 3

Sedangkan untuk listing programnya :


Nama Obyek Procedure
Command1 Click
if thisform.text1.value >= 60 then
thisform.label2.caption="Lulus "
else
thisform.label2.caption="Gagal "
endif
Command2 Click
with thisform
if thisform.text2.value>=80 then
.label9.caption="Nilai A"
else
if thisform.text2.value>=70 then
.label9.caption="Nilai B"
else
if thisform.text2.value>=60 then
.label9.caption="Nilai C"
else
if thisform.text2.value>=41 then
.label9.caption="Nilai D"
else
.label9.caption="Nilai E"
endif
endif
endif
endif
endwith
Command3 Click
with thisform
if .text3.value >=0 and .text3.value<=5 then
.label11.caption="Balita"
else
if .text3.value >=6 and .text3.value<=14 then
.label11.caption="Anak-anak"
else
if .text3.value >=15 and .text3.value<=23 then
.label11.caption="A B G"
else
if .text3.value >=24 and .text3.value<=35 then
.label11.caption="Dewasa"
else
if .text3.value >=36 and .text3.value<=60 then
.label11.caption="Orang Tua"
else
.label11.caption="Manula"
endif
endif
endif
endif
endif
endwith

Anda mungkin juga menyukai