Tipe Data Operator Struktur Kendali Struktur · PDF file1 Tipe Data Operator Struktur Kendali...
Transcript of Tipe Data Operator Struktur Kendali Struktur · PDF file1 Tipe Data Operator Struktur Kendali...
1
Tipe DataOperator
Struktur KendaliStruktur Pengulangan
Array
UNIVERSITAS WIDYATAMA
Teknik Informatika II-2 Universitas Widyatama
Overview
• Tujuan Instruksional
• Tipe Data
• Operator
• Variabel & Konstanta
• Struktur Kendali
• Struktur Pengulangan
• Studi Kasus
2
Teknik Informatika II-3 Universitas Widyatama
Tujuan Instruksional
Mahasiswa akan dapat
– Menjelaskan Type Data, Operator, Penamaan
Variabel, Deklarasi Variabel, dan konstanta
– Menggunakan Tipe Data beserta Operatornya
– Menyebutkan struktur Kendali dan Pengulangan
– Menjelaskan penggunaan struktur kendali dan
Pengulangan
– Menggunakan struktur kendali dan Pengulangan
Teknik Informatika II-4 Universitas Widyatama
Tipe Data
3
Teknik Informatika II-5 Universitas Widyatama
Tipe Data
Teknik Informatika II-6 Universitas Widyatama
Operator
4
Teknik Informatika II-7 Universitas Widyatama
Operator (1)
Arithmatic Operator
Teknik Informatika II-8 Universitas Widyatama
Operator (2)Precedence of Arithmatic Operator
5
Teknik Informatika II-9 Universitas Widyatama
Operator (3)Comparison Operator
Teknik Informatika II-10 Universitas Widyatama
Variabel & Konstanta
6
Teknik Informatika II-11 Universitas Widyatama
Scope of Variables
• Shows declarationsat form level, knownas: General Declarations
• Shows variabledeclarations withinan event handler
Form1
General Declarations
Sub Command1_Click ()
Sub Command2_Click ()
Dim sName1 As String
Dim iNum1 As Integer
Dim sName2 As String
Dim iNum2 As Integer
Dim sName3 As String
Dim iNum3 As Integer
Available variables:
sName1, sName2, iNum1, iNum2
Available variables:
sName1, sName3, iNum1, iNum3
Teknik Informatika II-12 Universitas Widyatama
Static Variables• A static variable will hold its value when it goes out of scope:
Sub Command1_Click()'declare variables
Dim iDimCount As IntegerStatic iStaticCount As Integer'increment variables
iDimCount = iDimCount + 1iStaticCount = iStaticCount + 1'display variablesLabel3.Caption = Str$(iDimCount)Label4.Caption = Str$(iStaticCount)
End Sub
• A static variable can only be declared inside a procedure• Note the use of a comment• Note use of Str$ function
7
Teknik Informatika II-13 Universitas Widyatama
Static Variables: Effects
• The effect of using a static variable can be seen below:
• This is has the same effect as if the variable had been declared at the form’s general declaration level
• Except the scope is local to the procedure
Teknik Informatika II-14 Universitas Widyatama
Deklarasi Variabel (1)
o Visual Basic memungkinkan kita untuk menggunakanvariabel tanpa deklarasi
o Deklarasi Variabel pada Visual Basic dapat dilakukandengan menggunakan keyword berikut :
8
Teknik Informatika II-15 Universitas Widyatama
Deklarasi Variabel (2)
Aturan penamaan Prosedur, fungsi, Variabel
dan konstanta :
– Harus dimulai dengan suatu huruf
– Tidak mengandung titik dan spesial karakter
– Tidak dapat lebih dari 255 karakter, khususnama kontrol, form, class dan modul tidak dapatlebih dari 40 karakter
– Bukan merupakan keyword
Teknik Informatika II-16 Universitas Widyatama
Deklarasi Variabel (3)
• Deklarasi Implicit
– Default deklarasi dari visual basic
– Bentuk deklarasi ini tidak disarankan karena dapat
menimbulkan bug-bug tersembunyi
• Deklarasi Explicit
– Setiap variable harus dideklarasikan terlebih dahulu
– Menambahkan perintah “option explicit”
– Harus diletakan di bagian deklarasi pada setiap form
dan class modul
9
Teknik Informatika II-17 Universitas Widyatama
Deklarasi Konstanta (1)
• Konstanta merupakan nama yang menyimpansuatu nilai yang tidak dapat berubah
• 2 sumber konstanta :– Instrinsic atau system-defined konstanta, merupakan
konstanta yang disediakan oleh suatu aplikasi ataukontrol
– Symbolic atau user-defined konstanta, merupakankonstanta yang dideklarasikan dengan menggunakanstatemen Conts
– Format :
[Public/Private] Const ConstantName [As Type] = expresion
Teknik Informatika II-18 Universitas Widyatama
Deklarasi Konstanta (2)
• Scope dari User-defined konstanta
– Hanya dikenal pada suatu prosedur (dideklarasikan
pada prosedur tersebut)
– Dikenal pada semua prosedur pada suatu modul(dideklarasikan pada suatu modul)
– Dikenal pada semua aplikasi (menambahkan keyword
Public sebelum keyword Const pada saat
pendeklarasian konstanta)
10
Teknik Informatika II-19 Universitas Widyatama
Struktur Kendali
Teknik Informatika II-20 Universitas Widyatama
Pendahuluan
o Struktur kendali memungkinkan programmer untukmengatur jalannya program
o Struktur kendali yang didukung Visual Basic adalahsebagai berikut :
o If-Then : digunakan untuk mejalankan pernyataankondisional
o Select Case : sama dengan struktur If-Then, namuncode lebih mudah dibaca.
11
Teknik Informatika II-21 Universitas Widyatama
Struktur If-Then
If-Then
Kondisi biasanya berupa suatu perbandingan maupun
ekspresi yang menghasilkan nilai numerik. Visual Basic menginterprestasikan False sebagai nol (0) dan True
sebagai bukan nol
Teknik Informatika II-22 Universitas Widyatama
Struktur If-Then-ElseIf-Then-Else
Visual Basic awalnya akan mencoba kondisi1, jika kondisi1
bernilai False maka Kondisi2 yang akan diperiksa danbegitu seterusnya sampai ditemukan kondisi bernilai True
untuk menjalankan blok pernyataan atau sampai akhir If.
12
Teknik Informatika II-23 Universitas Widyatama
Struktur Select-CaseSelect-Case
Struktur Case bekerja dengan suatu percobaan tunggal
yang hanya di evaluasi satu kali pada bagian atas struktur, dimana Visual Basic membandingkan hasil ekspresi
dengan nilai yang ada pada setiap case di dalam struktur
tersebut
Teknik Informatika II-24 Universitas Widyatama
StrukturPengulangan
13
Teknik Informatika II-25 Universitas Widyatama
Pendahuluan
o Struktur Pengulangan memungkinkan programmer untukmenjalankan sekolompok baris lebih dari satu kali
o Struktur Pengulangan yang didukung Visual Basic adalahsebagai berikut :
o For-Next : digunakan untuk mejalankan sekelompokatau suatu blok statemen dengan jumlah pengulanganyang telah ditetapkan sebelumnya
o Do-Loop : digunakan untuk menjalankan sekelompokatau suatu blok statemen dengan jumlah pengulangantak hingga.
Teknik Informatika II-26 Universitas Widyatama
Struktur For-NextFor-Next
o For-Next menggunakan sebuah variabel yang disebut counter yang
mana akan bertambah atau berkurang disetiap pengulangan
o Argumen counter, start, end dan increment semuanya bertipe numerik
o Pada saat menjalankan For, Visual Basic menset nilai counter sama
dengan start. Kemudian dicek apakah counter lebih dari end?
– Jika ya maka pengulangan akan dihentikan
– Jika tidak maka pengulangan akan dilaksanakan kembali sampai nilai
counter lebih dari end. Nilai counter akan ditambahkan atau dikurangidengan nilai increment
14
Teknik Informatika II-27 Universitas Widyatama
Struktur Do-LoopDo-Loop
o Pada Do-Whille-loop kondisi akan dicek dahulu pertama kali, dan
pada Do-Loop-While blok statemen akan dijanlankan terlebih dahulubaru mengecek kondisi yang ada. Jika kondisi bernilai benar (True) maka blok statemen akan dijalankan. Sebaliknya jika kondisi bernilaisalah (False) maka proses pengulangan akan dihentikan.
o Bagaimana dengan Do-Until-Loop dan Do-Loop-Until ?
Atau
Teknik Informatika II-28 Universitas Widyatama
Struktur Nested
o Merupakan suatu bentuk struktur kendali ataupengulangan yang terdiri dari lebih dari satustruktur kendali atau pengulangan
o Misalnya dalam struktur If-Then terdapat strukturkendali lain atau terdapat struktur pengulangan, contoh :If…..then
for……statemen
next….statemen
endif
15
Teknik Informatika II-29 Universitas Widyatama
Meninggalkan Struktur Kendaliatau Pengulangan
• Keyword Exit dan End digunakan untuk menghentikan
suatu proses tertentu atau keluar dari struktur kendali ataustruktur pengulangan.
• Keyword Exit terdiri dari :
– Exit-Do, digunakan pada semua struktur do-loop
– Exit-For, digunakan pada struktur for-next
Teknik Informatika II-30 Universitas Widyatama
ArrayVine, Michael; Microsoft Visual Basic Programming for Absolute Beginner; Prima Tech; http://www.prima-tech.com/
16
Teknik Informatika II-31 Universitas Widyatama
Introduction (1)
Teknik Informatika II-32 Universitas Widyatama
Introduction (2)
17
Teknik Informatika II-33 Universitas Widyatama
Introduction (2)
Teknik Informatika II-34 Universitas Widyatama
Single Dimension Array (1)
o A single dimension array contains a single row of like data.
o Declaring Single Dimension Array :1. [Public/Static/Dim/Private] ArrayName (number of
Elemets) as Data Type
2. [Public/Static/Dim/Private] Array Name (Lower Bound To Upper Bound) as Data Type
18
Teknik Informatika II-35 Universitas Widyatama
Single Dimension Array (2)
o Example :1. Dim MyArray(5) as Integer
2. Public ArrayOfMine(1 To 5) as String
Lower Bound = 0
Upper Bound = 5
There Are 6 elemets in MyArray
Lower Bound = 1
Upper Bound = 5
There Are 5 elemets in ArrayOfMine
Teknik Informatika II-36 Universitas Widyatama
Multidimensional Array (1)
Sometimes data cannot be accurately depicted and
stored in a single dimension array. Suppose you
want to store the x and y coordinates of a map or
grid or the rows and columns of a record from a file.
You can implement these and many other examples
through the use of a two-dimensional array. Also
known as multisubscripted arrays, multidimensional
arrays are not limited to just two dimensions.
Although it is not uncommon to have three- and
even four-dimensionalarrays, this section
concentrates on two-dimensional arrays only.
19
Teknik Informatika II-37 Universitas Widyatama
Multidimensional Array (2)
o Declaring Multidimensional Array :
1. [Public/Static/Dim/Private] ArrayName (number of Elemets-1, number of Elemets-2,…, number of Elemets-n) as Data Type
2. [Public/Static/Dim/Private] Array Name (Lower Bound-1 To Upper Bound-1, Lower Bound-2 To Upper Bound-2,…, Lower Bound-n To Upper Bound-n) as Data Type
Teknik Informatika II-38 Universitas Widyatama
Multidimensional Array (3)o Example : A Two-dimensional array with rows and
columns
Dim Arr(3,3)Of integer
20
Teknik Informatika II-39 Universitas Widyatama
Dynamic Array (1)• Dynamic arrays are similar to their cousins in that they share a
similar name, like data, and elements to access specific memory
locations. What makes dynamic arrays unique is their ability to be
resized. Essentially, they can increase and decrease at any time
during program execution.
• To declare a dynamic array, simply leave the bounds out of the
parentheses:
Dim dynamicArray()
• Once you declare the array, you can add elements dynamically
through the ReDim keyword. The ReDim keyword allocates the
number of elements to a dynamic array. Note that the ReDim
keyword is actually an executable statement and therefore must be
used in a procedure:
ReDim dynamicArray(4)
Teknik Informatika II-40 Universitas Widyatama
Dynamic Array (2)• Like static-array declarations, the ReDim statement can use variable
names for upper and lower bounds. In addition, you can set explicit
bounds in a ReDim statement:
ReDim dynamicArray(1 To 5, x To y)
• If you want to preserve the contents of a dynamic array while
increasing its size, use the Preserve keyword with ReDim. For
example, you create a two-dimensional array as follows:
ReDim dynamicArray(5, 5)
• Later, you need to increase the size of the array, but you do not want
to lose its contents. You could you the Preserve keyword to
dynamically increase the array’s size and maintain its values:
ReDim Preserve dynamicArray(5, 10)
• The Preserve keyword can only be used to increase an array’s
upper bound of its last dimension.
21
Teknik Informatika II-41 Universitas Widyatama
Control Array (1)• As you have seen, arrays can have many data types. In addition to
customary data types, Visual Basic supports the concept of control
arrays. You can think of this array type as an array of objects, or in
this case, an array of controls.
• Control arrays support all types of Visual Basic controls. For
instance, you could have an array of Labels, Command Buttons,
Option Buttons, Text Boxes, Picture Boxes, or Images, to name just
a few.
• Why would you want an array of controls? Well, in short, you might
want an array of controls for the same reason you would want any
other array, such as creating and maintaining less code or
combining like data (in this case, like controls).
• Beyond programming esthetics, control arrays offer the benefit of
shared procedures and events.
Teknik Informatika II-42 Universitas Widyatama
Control Array (1)• To create a control array in Visual Basic during design time, simply
add a control to your Form. Next, copy the control using either menu
options or keyboard shortcuts, and paste the copy in memory to the
same Form. A dialog box should appear asking whether you want to
create a control array. After you select Yes in the dialog box, the new
control is added and your control array is created. After which, you
can continue to add controls to the array by pasting until you
havethe desired amount.