Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
Diterbitkan olehHadi Budiman Telah diubah "6 tahun yang lalu
1
Algoritma & PEMROGRAMAN 2B (Visual basic)
Bonang Waspadadi Ligar, S.Si, MMSI
2
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
3
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.
5
Usage of Variables Menyalin dan menyimpan nilai-nilai
Melakukan manipulasi aritmatika Menguji apakah sebuah nilai memenuhi kriteria tertentu
6
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 E+38 to E-45 for negative values E-45 to E+38 for positive values. Double 8 bytes e+308 to E-324 for negative values E-324 to e+308 for positive values. Currency -922,337,203,685, to 922,337,203,685, Decimal 12 bytes +/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use +/ (28 decimal places).
7
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
8
Variable Names Karakter awal harus berupa huruf atau underscore
Harus berisi huruf, angka, dan garis bawah (tanpa spasi, titik, dll) Panjang nama bisa hingga karakter Namun yang dianjurkan adalah maksimum 32 karakter Tidak bisa berupa keyword bahasa VB
9
Variable Names Notasi Hungarian Misal: intMax, dblKeliling, sngLuas
Notasi baru Misal: firstName, luasPersegi, akarKuadrat
10
Variable Names Nama Valid _sum strProduct getSize Item_222 MAX_HOURS Nama Invalid 13Users get Size box-22 IsEmpty? Integer
11
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
12
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
13
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
14
Variable scope 1. Local Scope:
Sebuah variabel yang dinyatakan dalam prosedur Variabel ini hanya dapat diakses dalam prosedur di mana ia dideklarasikan.
15
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
16
Variable scope 2. Global Scope:
Sebuah variabel yang dinyatakan diluar modul maupun prosedur Variabel ini hanya dapat diakses semua prosedur
17
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 = D Const intMAXHOURS As Integer = 40 Private Const strCOTITLE As String = "ABC Company"
18
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)
19
Common Arithmetic Operators
Examples of use: decTotal = decPrice + decTax decNetPrice = decPrice - decDiscount dblArea = dblLength * dblWidth sngAverage = sngTotal / intItems dblCube = dblSide ^ 3
20
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
21
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
22
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
23
All Operators Precedence
Parenthesis Exponential Multiplication / Division Integer Division MOD Addition / Subtraction String Concatenation Relational Operators (< , > , >= , <= , <>) Logical Operators (AND, OR, NOT)
24
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
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.