Tipe Data
Operator
Struktur Kendali
Struktur Pengulangan
Array
UNIVERSITAS
WIDYATAMA
Overview
• Tujuan Instruksional
• Tipe Data
• Operator
• Variabel & Konstanta
• Struktur Kendali
• Struktur Pengulangan
• Studi Kasus
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-5 Universitas Widyatama
Tipe Data
Teknik Informatika II-7 Universitas Widyatama
Operator (1)
Arithmatic Operator
Operator (2)
Teknik Informatika II-9 Universitas Widyatama
Operator (3)
Comparison Operator
Variabel &
Konstanta
Teknik Informatika II-11 Universitas Widyatama
Scope of Variables
• Shows declarations
at form level, known as: General Declarations • Shows variable declarations within an 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
Static Variables
• A static variable will hold its value when it goes out of scope: Sub Command1_Click()
'declare variables Dim iDimCount As Integer
Static iStaticCount As Integer 'increment variables iDimCount = iDimCount + 1 iStaticCount = iStaticCount + 1 'display variables Label3.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
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
Deklarasi Variabel (1)
o Visual Basic memungkinkan kita untuk menggunakan variabel tanpa deklarasi
o Deklarasi Variabel pada Visual Basic dapat dilakukan dengan menggunakan keyword berikut :
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, khusus
nama kontrol, form, class dan modul tidak dapat
lebih dari 40 karakter
– Bukan merupakan keyword
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
Teknik Informatika II-17 Universitas Widyatama
Deklarasi Konstanta (1)
• Konstanta merupakan nama yang menyimpan
suatu nilai yang tidak dapat berubah
• 2 sumber konstanta :
– Instrinsic atau system-defined konstanta, merupakan konstanta yang disediakan oleh suatu aplikasi atau kontrol
– Symbolic atau user-defined konstanta, merupakan konstanta yang dideklarasikan dengan menggunakan statemen Conts
– Format :
[Public/Private] Const ConstantName [As Type] = expresion
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)
Teknik Informatika II-19 Universitas Widyatama
Struktur Kendali
Pendahuluan
o Struktur kendali memungkinkan programmer untuk mengatur jalannya program
o Struktur kendali yang didukung Visual Basic adalah sebagai berikut :
o If-Then : digunakan untuk mejalankan pernyataan kondisional
o Select Case : sama dengan struktur If-Then, namun code lebih mudah dibaca.
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
Struktur If-Then-Else
If-Then-Else
Teknik Informatika II-23 Universitas Widyatama
Struktur Select-Case
Select-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
Struktur
Pengulangan
Teknik Informatika II-25 Universitas Widyatama
Pendahuluan
o Struktur Pengulangan memungkinkan programmer untuk menjalankan sekolompok baris lebih dari satu kali
o Struktur Pengulangan yang didukung Visual Basic adalah sebagai berikut :
o For-Next : digunakan untuk mejalankan sekelompok atau suatu blok statemen dengan jumlah pengulangan yang telah ditetapkan sebelumnya
o Do-Loop : digunakan untuk menjalankan sekelompok atau suatu blok statemen dengan jumlah pengulangan tak hingga.
Struktur For-Next
For-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
Teknik Informatika II-27 Universitas Widyatama
Struktur Do-Loop
Do-Loop
o Pada Do-Whille-loop kondisi akan dicek dahulu pertama kali, dan pada Do-Loop-While blok statemen akan dijanlankan terlebih dahulu baru mengecek kondisi yang ada. Jika kondisi bernilai benar (True) maka blok statemen akan dijalankan. Sebaliknya jika kondisi bernilai salah (False) maka proses pengulangan akan dihentikan.
o Bagaimana dengan Do-Until-Loop dan Do-Loop-Until ? Atau
Struktur Nested
o Merupakan suatu bentuk struktur kendali atau
pengulangan yang terdiri dari lebih dari satu
struktur kendali atau pengulangan
o Misalnya dalam struktur If-Then terdapat struktur
kendali lain atau terdapat struktur pengulangan,
contoh :
If…..then
for……
statemen
next….
statemen
Teknik Informatika II-29 Universitas Widyatama
Meninggalkan Struktur Kendali
atau Pengulangan
• Keyword Exit dan End digunakan untuk menghentikan suatu proses tertentu atau keluar dari struktur kendali atau struktur pengulangan.
• Keyword Exit terdiri dari :
– Exit-Do, digunakan pada semua struktur do-loop – Exit-For, digunakan pada struktur for-next
Array
Teknik Informatika II-31 Universitas Widyatama
Introduction (1)
Teknik Informatika II-33 Universitas Widyatama
Introduction (2)
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
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
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
Teknik Informatika II-37 Universitas Widyatama
Multidimensional Array (2)
o Declaring Multidimensional Array :
1. [Public/Static/Dim/Private] ArrayName (number of 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
Multidimensional Array (3)
o Example : A Two-dimensional array with rows and columns
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)
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.
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.
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.