Pertemuan 03 Representing Data Matakuliah : D0524 / Algoritma dan Pemrograman Komputer

advertisement
Matakuliah
Tahun
Versi
: D0524 / Algoritma dan Pemrograman Komputer
: 2005
:
Pertemuan 03
Representing Data
1
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Menunjukkan representasi data, konstanta dan
variabel
2
Outline Materi
• Data Categorization
• Constants
• Variables
3
Data Categorization
• Two broad categories of data are numeric
and string.
– Numeric data must contain only numbers.
– String data can contain any symbol.
– Numeric data is used in arithmetic
calculations.
– String data cannot be used in calculations.
4
Constants
• Data item whose value is assigned at design
time and remains the same at run time.
• A literal constant is just a value.
• A symbolic constant is a descriptive name
substituted for a literal constant.
• Four different kinds of constants:
–
–
–
–
Numeric literal.
String literal.
Numeric symbolic.
String symbolic.
5
Literal Constans
• Writing Numeric Literal Constants
– Ex. 3.45
+231
.1
9.4E+7
• Writing String Literal Constants
– Ex. “Hello Jean”
• Symbolic Constants
– INTERESTRATE represents 0.045.
• Creating/Choosing Symbolic Constant Names
– Names are chosen by the developer.
– Naming rules must be adhered to.
• The Constant Definition Statement
– Ex. Const INTERESTRATE = 0.045
6
Literal Constans
• Run Time: How the Computer Uses Symbolic Constants
– Stored in a reference table for later use.
• Advantages of Using Symbolic Constants
– Make program easier to understand.
– Reduce the chance of program inconsistencies.
• Literal versus Symbolic Constants
– The null string (“”) and numeric data used in formulas should be
the only literal constants.
• Typical Uses of Symbolic Constants
– Prime interest rate.
– Overtime rate.
– Number of print lines for a printed page.
• Predefined Symbolic Constants
– Visual Basic contains a large set.
– Contained in classes, such as the Math and Color classes.
7
Data Types
Data type
Byte
Description
Range
1-byte binary data
0 to 255
Integer
2-byte integer
– 32,768 to 32,767
Long
4-byte integer
– 2,147,483,648 to 2,147,483,647
Single
4-byte floating-point number
Double
8-byte floating-point number
– 3.402823E38 to – 1.401298E – 45 (negative values)
1.401298E – 45 to 3.402823E38 (positive values)
– 1.79769313486231E308 to – 4.94065645841247E – 324 (negative values)
4.94065645841247E – 324 to 1.79769313486231E308 (positive values)
8-byte number with fixed decimal point
– 922,337,203,685,477.5808 to 922,337,203,685,477.5807
String
String of characters
Zero to approximately two billion characters
Variant
Date/time, floating-point number,
integer, string, or object.
16 bytes, plus 1 byte for each character
if a string value.
Date values: January 1, 100 to December 31, 9999
Numeric values: same range as Double
String values: same range as String Can also contain Error or Null values
Boolean
2 bytes
True or False
8-byte date/time value
January 1, 100 to December 31, 9999
4 bytes
Any Object reference
Currency
Date
Object
8
Declaring Variables
•
A variable declaration statement.
– Examples:
•
•
•
•
•
•
Dim StreetAddress As String
Dim GrossWeight As Integer
Dim HomePhone As String
Dim NetIncome As Decimal
Dim CurrentStudent As Boolean
Syntax of the Assignment Statement
– variablename = expression
• Ex. CourseCode = “CISB119”
•
Run Time: The Effect of the Assignment Statement
– Evaluates expression on right side of equal sign.
– Stores value of expression in variable on left side of equal sign.
•
Changing Variable Values during Execution
– Storing a value in a variable will overwrite any existing value.
•
Control Properties in Assignment Statements
– Ex. lblHomePrice.Text = 210000
•
Why Use Variables
– Ideal to store results of intermediate calculations.
– Values stored in variables may be retrieved and formatted.
9
Variable Scope
• The domain within which a variable can be
accessed.
• Set of all the code that can refer to a
variable.
• Determined by where and how the
variable is declared.
• There are four levels: block, procedure
(local), module, and global.
10
Variable Scope
• Block and Procedure Level Scope:
– Any variable declared inside a procedure has
procedure-level scope.
– Variable can only be accessed by statements in that
procedure.
– Scope can be narrowed to a single block of code
within the procedure.
• Module Level Variables:
– Any variable declared inside a procedure has
procedure-level scope.
– Variable can only be accessed by statements in that
procedure.
– Scope can be narrowed to a single block of code
within the procedure.
11
Variable Scope
• Global Variable:
– Variables that can be shared across all forms have global scope.
– The Public Statement
• Used to create a global variable.
– Modules
• Repository for data that need to be shared by forms.
• Repository for global variables.
– Hiding Global Variables
• A procedure-level variable in a procedure “hides” a global variable with the
same name.
• A module-level variable will “hides” a global variable with the same name.
– Procedure-Level, Module-Level, and Global Scope
• A procedure-level variable is declared in a procedure using a Dim statement.
• A module-level variable is declared in the declarations section of a form
using a Dim statement.
• A global variable is declared in the declarations section of a module using
the Public statement.
12
Project Structure
• Project
– Forms
• General Declarations Section
• Controls
– Properties
– Event Procedures
– Methods
– Code Modules
• General Declarations Section
13
Variable Naming Convention
• Must begin with a letter
• Cannot have a period (remember that we use the period
to set a property; in other words the period is an
operator)
• Can have up to 255 characters. Please, just because it is
allowed, don't use 255 characters.
• Must be unique inside of the procedure or the module it
is used in
• Must not be a VB keyword.
14
Download