Berdasarkan pengalaman pemrograman, pemakaian operator tidak dapat
dilepaskan dari pekerjaan pemrograman, adapun operator pada Visual Basic adalah
sebagai berikut :
Operator aritmatika : *, /, \, ^, Mod,
+, dan –
Operator relasi : =, <>,
<, >, <=, and >=
Catatan :
Pada tipe data string, prilaku perbandingan
sangat dipengaruhi oleh setting dari Option Compare. (default adalah Option
Compare Binary)
Pada Option Compare Binary : A < B < E
< Z < a < b < e < z < À < Ê < Ø < à < ê < ø
Pada Option Compare Text : (A=a) < (À= à) < (B=b) < (E=e)
< (Ê= ê) < (Ø = ø) < (Z=z)
|
Operator logika And, AndAlso, Or, OrElse, and Xor
Catatan :
|
Operator pengabungan : &
Operator bit shift : << dan >>
Contoh :
Dim Pattern As Short = 192 ' Bit pattern is 0000 0000 1100 0000.
Dim Result1, Result2, Result3, Result4, Result5
As Short
Result1 = Pattern << 0 ' Result is 192 (0000 0000 1100 0000).
Result2 = Pattern << 4 ' Result is 3072 (0000 1100 0000 0000).
Result3 = Pattern << 9 ' Result is -32768 (1000 0000 0000 0000).
Result4 = Pattern << 17 ' Result is 384 (0000 0001 1000 0000).
Result5 = Pattern << -1 ' Result is 0 (shifted
15 places to left).
Dim Pattern As Short = 2560 ' Bit pattern is 0000 1010 0000 0000.
Dim Result1, Result2, Result3, Result4, Result5
As Short
Result1 = Pattern >> 0 '
Result is 2560 (0000 1010 0000 0000).
Result2 = Pattern >> 4 '
Result is 160 (0000 0000 1010 0000).
Result3 = Pattern >> 10 '
Result is 2 (0000 0000 0000 0010).
Result4 = Pattern >> 18 '
Result is 640 (0000 0010 1000 0000).
Result5 = Pattern >> -1 '
Result is 0 (shifted 15 places to
right).
|
Catatan :
Operator bit shift bekerja pada tingkat bit, yang artinya jika 192
pada tipe data short (desimal/basis sepuluh) maka representasinya dalam
bentuk biner adalah 0000 0000 1100 0000 (16 bit).
Kemudian hal lain yang perlu diketahui adalah bit mask untuk
masing-masing tipe data adalah berbeda, berikut ini adalah tabel bitmask :
Perhatikan kembali contoh diatas
Dim Pattern As Short = 2560 '
Bit pattern is 0000 1010 0000 0000.
Perintah shift right 18 untuk tipe Short akan menghasilkan operasi
yang sama dengan shift right 2 ( hasil operasi 18 And 15 = 2).
Result4 = Pattern >> 18 ' Result is 640 (0000 0010 1000 0000).
|
Operator perbandingan Like, dengan aturan sebagai berikut :
- Karakter ? mewakili satu karakter sembarang.
- Karakter * mewakili tidak ada karakter atau sekumpulan karakter sembarang.
- Karakter # mewakili satu sembarang digit (0–9).
- Suatu daftar karakter dalam kurung
siku ([
ab
]) mewakili salah satu karakter dalam daftar tersebut. - Suatu daftar dari karakter dalam
kurung siku dan diawali dengan suatu tanda ! ([!
ab
]) mewakili satu karakter yang tidak ada dalam daftar tersebut.
Dim myCheck As Boolean
|
|
myCheck = "F" Like "F"
|
Does "F" match "F"? Returns True.
|
myCheck = "F" Like "f"
|
Does "F" match "f"? Returns False.
|
myCheck = "F" Like "FFF"
|
Does "F" match "FFF"? Returns False.
|
myCheck = "aBBBa" Like "a*a"
|
Does "aBBBa" have a "a" at the beginning, an
"a" at the end, and any number of characters in between? Returns True.
|
myCheck = "F" Like "[A-Z]"
|
Does "F" occur in the set of characters from A to Z? Returns True.
|
myCheck = "F" Like "[!A-Z]"
|
Does "F" NOT occur in the set of characters from A to Z?
Returns False.
|
myCheck = "a2a" Like "a#a"
|
Does "a2a" begin and end with an "a" and have any
single-digit number inbetween? Returns True.
|
myCheck = "aM5b" Like "a[L-P]#[!c-e]"
|
Does "aM5b" fit the following pattern: Begins with "a", has and
character from the set L through P, followed byb any single-digit number, and
finally contains any character excluded from the character set c through e.
Returns True.
|
myCheck = "BAT123khg" Like "B?T*"
|
Does "BAT123khg" fit the
following pattern: Begins with "B", followed by any single
character, followed by a "T" and finally zero or more characters of
any type. Returns True
|
myCheck = "CAT123khg" Like "B?T*"
|
Does "CAT123khg" fit the following pattern: Begins with
"B", followed by any single character, followed by a "T"
and finally zero or more characters of any type. Returns False.
|
Contoh :
Dim a As Integer = 2
Dim b As Integer = 3
a += b //
akan menghasilkan 5 pada variabel a, atau sama dengan a = a+b
|
Catatan :
Sesuatu hal yang penting bagi programmer pemula adalah memahami
urutan operasi dari operator, pada Visual Basic menggunakan urutan operasi
berikut ini :
Operator
Aritmatika dan Pengabungan
Exponentiation (^)
Unary negation (–)
Multiplication and division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction (+, –), string concatenation (+)
String concatenation (&)
Arithmetic bit shift (<<, >>)
Operator
Perbandingan
All comparison operators (=, <>, <, <=, >, >=,
Like, Is, TypeOf...Is)
Operator Logika
dan Bitwise
Negation (Not)
Conjunction (And, AndAlso)
Disjunction (Or, OrElse, Xor)
Contoh :
Dim A, B, C, D, E, F, G As Double
A = 3.0
B = 6.0
C = 4.0
D = 2.0
E = 1.0
F = A + B - C / D * E
' The previous line sets F to 7.0. Because of
natural operator
' precedence, it is exactly equivalent to the
following line:
F = (A + B) - ((C / D) * E)
' The following line overrides the natural
operator precedence:
G = A + (B - C) / (D * E)
' The previous line sets G to 4.0.
Dengan memahami urutan operasi akan sangat membantu untuk menghindari
kesalahan dalam penulisan rumus yang dapat menyebabkan logika error.
|