Transcript Chapter 4

Chapter 4
Variables and Constants
Variables
• In math x = 5,
x is the variable
• In Computer Programming we create
the variable first
• Stored memory location
• Declaration Statement
• Dim snglength As Single
Variables
• Several types of variables
– Single-stores numeric values, may be a
decimal
– Prefix is sng
• Initialized to equal zero
• Group like variables
• Declare, assign, reassign
Variable Assignment
• Declare
– Dim sngRadius As Single = 10
– sngRadius = 12.3
• Using variables
– sngCircleArea = 3.14 * sngRadius ^ 2
TextBox
•
•
•
•
Obtain values from the user
VB
Prefix – txt
Create a prompt – label
How are we going to store information that
is inputted?
• sngRadius = me.txtradius.text
• Create and test
Textbox
• Val function
• sngRadius = Val(me.txtradius.text)
• Ensure the variable matches the data type
– sngRadius = me.txtradius.text, and it’s a user
name
• If the textbox is not a number Val will
return a 0.
• TextChanged event procedure
Constants
• Exactly like a declaration statement..
– Const sngPi As Single = 3.14
• Values that are never changed
• Used exactly like a variable
• Something that does not have an obvious
name should not be a constant or a
variable such as sngradius ^ 2, the
squared portion is not obvious.
Data Types
Short
srt, integers
Integer
int, integers
Long
lng, large integers
Single
sng, numbers, decimals
Double
dbl, large numbers, decimals
Decimal
dec, many significant digits
Data Types
Date
dtm, month/day/year
Char
chr, single character/symbol
String
str, set of characters
Boolean
bln, represents true or false
Data Type
• Double check the data type: Integers
round to the nearest whole number
• Dim intX As Integer = 6.7
– intX = 7
Scope
• Global and Local Variables
• Scope of a Variable – is the set of statements
that can use the variable.
• Local Declaration – a declaration at the
beginning of a procedure where the variable can
only be used “locally”
• Global Declaration – a declaration placed at
the beginning of the program that can be used in
any procedure throughout the program.
Division Operators
• Integer division returns the whole number
– intX = 20\7,
‘intX = 2
• Modulus division returns the remainder
– intX = 20 mod 7, ‘intX = 6
Programming Errors
• Three types of errors
– Syntax errors
– Logic Errors
– Run-Time errors
Creating Breakpoints
• Stopping points to analyze the computer
thought process
• Program execution stops at the first
breakpoint
• Either Add Watch or view the autos
window
• VB