Algoritma & PEMROGRAMAN 2B (Visual basic) Bonang Waspadadi Ligar, S.Si, MMSI
Variables Sebuah lokasi penyimpanan dalam memori (RAM) Menyimpan data/informasi ketika program sedang berjalan Lokasi penyimpanan ini dapat dirujuk dengan nama-nama variabel Tiap variable memiliki 3 property: Nama – acuan lokasi – Tidak bisa di ubah Value – Informasi yang disimpan – bisa di ubah ketika program sedang berjalan. Data Type – Tipe/jenis data yang bisa disimpan – tidak bisa di ubah
Variables Progammer menentukan nama dari variable. Visual Basic mengaitkan nama itu dengan lokasi di RAM computer Nilai yang terkait dengan variable disimpan pada lokasi memori tersebut. Cukup menggunakan nama variable untuk mengakses nilai sebuah variable.
Usage of Variables Menyalin dan menyimpan nilai-nilai Melakukan manipulasi aritmatika Menguji apakah sebuah nilai memenuhi kriteria tertentu
Visual Basic Data Types (NUMERIC) Storage Range of Values Byte 1 byte 0 to 255 Integer 2 bytes -32,768 to 32,767 Long 4 bytes -2,147,483,648 to 2,147,483,648 Single -3.402823E+38 to -1.401298E-45 for negative values 1.401298E-45 to 3.402823E+38 for positive values. Double 8 bytes -1.79769313486232e+308 to -4.94065645841247E-324 for negative values 4.94065645841247E-324 to 1.79769313486232e+308 for positive values. Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807 Decimal 12 bytes +/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use +/- 7.9228162514264337593543950335 (28 decimal places).
Visual Basic Data Types (non-NUMERIC) Storage Range String(fixed length) Length of string 1 to 65,400 characters String(variable length) Length + 10 bytes 0 to 2 billion characters Date 8 bytes January 1, 100 to December 31, 9999 Boolean 2 bytes True or False Object 4 bytes Any embedded object Variant(numeric) 16 bytes Any value as large as Double Variant(text) Length+22 bytes Same as variable-length string
Variable Names Karakter awal harus berupa huruf atau underscore Harus berisi huruf, angka, dan garis bawah (tanpa spasi, titik, dll) Panjang nama bisa hingga 16383 karakter Namun yang dianjurkan adalah maksimum 32 karakter Tidak bisa berupa keyword bahasa VB
Variable Names Notasi Hungarian Misal: intMax, dblKeliling, sngLuas Notasi baru Misal: firstName, luasPersegi, akarKuadrat
Variable Names Nama Valid _sum strProduct getSize Item_222 MAX_HOURS Nama Invalid 13Users get Size box-22 IsEmpty? Integer
Declaring a Variable Pernyataan yang membuat sebuah variable dalam memori Syntax: Dim VariableName As DataType [=initial value] Dim (short for Dimension) - keyword VariableName – nama variabel As - keyword DataType – tipe data yang dikandung variabel Initial Value – nilai awal variabel Example: Dim intLength as Integer = 5
Default Values for Data Types Data type Default (Initial) value All numeric types Zero (0) Boolean False Char Binary 0 String or Object Empty Date 12:00 a.m. on January 1, 0001
Assignment Statement Syntax: variablename = expression Assigns the value of the expression to the variable. (The variable must be on the left and the expression on the right.) Example: intNumber1 = 4 intNumber2 = 3 * (2 + 2) intNumber3 = intNumber1 IntNumber1 = intNumber1 + 6 hhhhhhh
Variable scope 1. Local Scope: Sebuah variabel yang dinyatakan dalam prosedur Variabel ini hanya dapat diakses dalam prosedur di mana ia dideklarasikan.
Variable scope 2. Modul / Class Scope: Sebuah variabel yang dinyatakan dalam sebuah modul, namun diluar prosedur Variabel ini hanya dapat diakses oleh semua prosedur dalam modul
Variable scope 2. Global Scope: Sebuah variabel yang dinyatakan diluar modul maupun prosedur Variabel ini hanya dapat diakses semua prosedur
Constants Konstanta adalah variable yang hanya memiliki 1 nilai (tidak bisa diubah saat program berjalan) Syntax: Const constantname [As datatype] = expression Examples: Const decPI As Decimal = 3.141593D Const intMAXHOURS As Integer = 40 Private Const strCOTITLE As String = "ABC Company"
Performing Calculations with Variables Arithmetic Operators ^ Exponential * Multiplication / Floating Point Division \ Integer Division MOD Modulus (remainder from division) + Addition – Subtraction & String Concatenation (putting them together)
Common Arithmetic Operators Examples of use: decTotal = decPrice + decTax decNetPrice = decPrice - decDiscount dblArea = dblLength * dblWidth sngAverage = sngTotal / intItems dblCube = dblSide ^ 3
Special Integer Division Operator The backslash (\) is used as an integer division operator The result is always an integer, created by discarding any remainder from the division Example intResult = 7 \ 2 ‘result is 3 shrHundreds = 157 \ 100 ‘result is 1
Combined Assignment Operators Often need to change the value in a variable and assign the result back to that variable For example: var = var – 5 Subtracts 5 from the value stored in var Operator Usage Equivalent to Effect += x += 2 x = x + 2 Add to -= x -= 5 x = x – 5 Subtract from *= x *= 10 x = x * 10 Multiply by /= x /= y x = x / y Divide by \= x \= y x = x \ y Int Divide by &= x &= “.” x = x & “.” Concatenate
Arithmetic Operator Precedence Operator precedence tells us the order in which operations are performed From highest to lowest precedence: Exponentiation (^) Multiplicative (* and /) Integer Division (\) Modulus (MOD) Additive (+ and -) Parentheses override the order of precedence Where precedence is the same, operations occur from left to right
All Operators Precedence Parenthesis Exponential Multiplication / Division Integer Division MOD Addition / Subtraction String Concatenation Relational Operators (< , > , >= , <= , <>) Logical Operators (AND, OR, NOT)
Precedence Examples 6 * 2 ^ 3 + 4 / 2 = 50 7 * 4 / 2 – 6 = 8 5 * (4 + 3) – 15 Mod 2 = 34 intX = 10 intY = 5 intResultA = intX + intY * 5 'iResultA is 35 iResultB = (intX + intY) * 5 'iResultB is 75 dResultA = intX - intY * 5 'dResultA is -15 dResultB = (intX - intY) * 5 'dResultB is 25