Upload
dzung-bong
View
217
Download
0
Embed Size (px)
Citation preview
7/31/2019 Day04 01 Variables
1/37
By Lanka RodrigoBy Lanka Rodrigo
7/31/2019 Day04 01 Variables
2/37
OutlineOutline
,
Declaring and using variables Declaring constants
Common data types used in Math
Integers and Doublers (Floating point numbers)
Mathematical o erations
2Programming I
7/31/2019 Day04 01 Variables
3/37
VariablesVariables A variable is a named piece of memory (Temporary
Which has been specifically reserved for holding data in
A variable is a data box
Variable binds the stored data to a name which enables
convenient access at a letter time
Rules for naming variables
No more than 40 characters
Can only use letters, numbers and underscore (_) characters
The first character must be a letter
3Programming I
You cannot use the Visual Basic .NET key words (eg. Form, Beep)
7/31/2019 Day04 01 Variables
4/37
Variables Contd.Variables Contd.
It's variable, the content may vary"
Box that can only contain one thing at a time,
but the content of the box may change over time We use variables to store and keep track of things
May have many boxes, and to keep them apart,
we usually name them separately This is called "variable names
To make sure you can find a contents easily, always name it
so that the name explains what is in the box When you find it, it may turn out to be another,
just with somewhat similar content
4Programming I
7/31/2019 Day04 01 Variables
5/37
Variables Contd.Variables Contd.
Variables can be assignedvalues,via assignment:
Such an assignment operation takes the form:
Variable Name = New Value
Example:
5Programming I
7/31/2019 Day04 01 Variables
6/37
Declaration of VariablesDeclaration of Variables
Prior to use, variables must first be declared
This reserves (allocate) a place in computer memory
The VB syntax for a variable declarationDim Variable_Name
DimVariable_NameAsData_Type
Dim (Dimension): keyword for declaring a variable inside a
procedure.
As: Keyword for specifying the data type
6Programming I
7/31/2019 Day04 01 Variables
7/37
Data T eData T e If you think of a variable as a box for holding data,
.
Name Contains Type
USB_Box USB memory chips SolidMilk_Bottle Milk Liquid
en_ ox ens o
Tea_Bottle Tea Liquid
_
Coin_Box Coins Solid
7Programming I
7/31/2019 Day04 01 Variables
8/37
Data T eData T e A box's size and shape determine what the box can hold
" " ' y
ome use u a a ypes: Integer Non decimal numbers
String A set of letters or words Boolean True or false
Single Numbers with decimals
DateTime Date and time of day Decimal Decimal numbers
8Programming I
7/31/2019 Day04 01 Variables
9/37
Data T eData T e We need to handle data such as names, addresses, money, date,
,
Similarly in Visual Basic 2008, have to deal with all sorts of data,
some can be mathematicall calculated
while some are in the form of text or other forms
VB2008 divides data into different t es so that it is easier to
manage when we need to write the code involving those data
Visual Basic 2008 Data Types
Numeric data types andNon-numeric data types
9Programming I
7/31/2019 Day04 01 Variables
10/37
Numeric DataNumeric Data T eT e Numeric data types are types of data that consist of numbers,
with various standard operators
such as add, minus, multiply, divide and so onType Storage Range of Values
Byte 1 byte 0 to 255
Integer 2 bytes -32,768 to 32,767
- , , , , , ,
Single 4 bytes-3.402823E+38 to -1.401298E-45 for negative values
1.401298E-45 to 3.402823E+38 for positive values.
Double 8 bytes- . e o - . - or nega ve va ues4.94065645841247E-324 to 1.79769313486232e+308 for positive values.
Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807
10Programming I
Decimal 12 bytes
+/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use
+/- 7.9228162514264337593543950335 (28 decimal places).
7/31/2019 Day04 01 Variables
11/37
NonNon--numeric Datanumeric Data T eT e Nonnumeric data types are data that cannot be manipulated
The non-numeric data comprises Text or String, Date, Boolean
Ob ect and Variant data t e
Type Storage Range
,
String(variable length) Length + 10 bytes 0 to 2 billion characters
ate ytes anuary , to ecem er ,
Boolean 2 bytes True or False
Object 4 bytes Any embedded object
Variant(numeric) 16 bytes Any value as large as Double
11Programming I
Variant(text) Length+22 bytes Same as variable-length string
7/31/2019 Day04 01 Variables
12/37
Exam le of Variable UseExam le of Variable Use
Declare three variables as Inte er
12Programming I
7/31/2019 Day04 01 Variables
13/37
Data Types for NumbersData Types for Numbers When working with numbers, we use two types of data:
Integers (usually take the Integer type): xamp e: , , ,
Useful for discrete math:
counting objects (cardinal) .
ordering objects (ordinal)
Example: The one-hundredth customer will win
Floating point numbers (usually take the Double type): Example: 1.50, 3.1415926, etc have a decimal point
More useful for normal arithmetic
Note: If you do not explicitly declare a variables data type It is declared by the system as an Object, by default
We will talk more about Integers and Floats, and other data types,shortly
13Programming I
First, lets look at some basic mathematical operations
7/31/2019 Day04 01 Variables
14/37
ConstantsConstants Assigning avariable fixes a memory location for
storage
However, within limits a variable may take arbitrary
values Depending on the data type (i.e., Integer, Double, etc)
The value of a constant, on the other hand, is fixed.
The VB syntax for declaring a constant is:
ons const_name s ata_ ype= va ue
Const is a keyword declaring the constant; Const_name andvalue are the name and value of the constant;
Data_Type is the type of constant
14Programming I
7/31/2019 Day04 01 Variables
15/37
Constants Contd.Constants Contd.
This is called
initialization
xamp e: m x s n eger =
Declares Integer variable x, and sets its starting value to 6
ome exam es:
Const x As Integer = 10
onst s ou e = .
15Programming I
7/31/2019 Day04 01 Variables
16/37
Data T e conversionData T e conversion
In our programs we may want to convert our data type
o a a o e y e. s co ve s o s ca e as
We use CType function for the conversionExamples:
CBool - use this function to convert to Bool data type
CByte - use this function to convert to Byte data type
CChar - use this function to convert to Char data type
CDate - use this function to convert to Date type
CDbl - use this function to convert to Double data type
16Programming ICDec - use this function to convert to Decimal data type
7/31/2019 Day04 01 Variables
17/37
Data T e conversionData T e conversion
Examples:
CInt - use this function to convert to Integer data type
CLng - use this function to convert to Long data type
COb - use this function to convert to Ob ect t e
CShort - use this function to convert to Short data type
CSng - use this function to convert to Single data type
r ng - use s unc on o conver o r ng a a ype
17Programming I
7/31/2019 Day04 01 Variables
18/37
Pro ramPro ram Sim le Messa eSim le Messa e Make a simple program to get a return Message
18Programming I
7/31/2019 Day04 01 Variables
19/37
Pro ramPro ram Sim le Messa eSim le Messa e
Step 01 Control Arrangements
tep ett ng ropert es
19Programming I
7/31/2019 Day04 01 Variables
20/37
Pro ramPro ram Sim le Messa eSim le Messa e
20Programming I
7/31/2019 Day04 01 Variables
21/37
Pro ramPro ram Sim le Messa eSim le Messa e
21Programming I
7/31/2019 Day04 01 Variables
22/37
Mathematical O erationMathematical O eration
The table below contains the operators available for basic math
operation
22Programming I
7/31/2019 Day04 01 Variables
23/37
Math StatementsMath Statements What does the statement, x = x + 1 do?
Thinking in terms of arithmetic, this is a nonsense statement
Since = is defined as e ualit
But x is never equal to x + 1!
However, if we instead think in VB, it makes perfect sense! emem er = s t e ass gnment operator
Thus, x = x + 1 tells the computer to:
First, get the value stored in variable x
Then, add 1 to this value Lastly, store the result in variable x
,
Dim x As Integer = 10
x = x + 1
ur ng run- me, e r g s e s rs eva ua e o y e
Then, this result (11) ispassed to the left side (x)
So, the overall result is to set:
23Programming I
x = 11
7/31/2019 Day04 01 Variables
24/37
Assignment OperatorsAssignment Operators
Simple one-variable expressions, such as:
Are really assignment operations,
Short-hand operators exist for such operations:
Which combine both o erators into a sin le assi nment
operation For instance, the statement,
n = n + 8 can be written as: n += 8
Either form assigns the valuen + 8
to the variable n
Short-hand operators exist for all 4 basic operations:
+=, -=, *=, /=
24Programming I
7/31/2019 Day04 01 Variables
25/37
Math Statements (Contd.)Math Statements (Contd.) More generally, a math statement takes the form:
left_side = right_side
Where, left_side is a variable
While right_side is a mathematical expression. t run-t me, t e r g t_s e s eva uate
And thenpassed to the left_side
For instance as a result of the statement: z = 2 * 3
First, the right side is evaluated (yielding 6) Then, the result is passed to z (setting z equal to 6)
What about a compound statement (several math ops):x = 3 * 2 + 1 ?
x = 6 + 1 = 7
If we add first, we get :
25Programming I
x = =
Which is correct?
7/31/2019 Day04 01 Variables
26/37
Operator PrecedenceOperator Precedence
In VB, the order of evaluation of math operators is determined by
precedence
For arithmetic, the order of evaluation is (first to last):
Exponentiation (^)
,
Such as the - in x = -6
Multiplication and floating-point division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction +,
Note that operations on strings come next (more, later):
String concatenation (+)
tr ng concatenat on &
So, for our example:
x = 3 * 2 + 1
26Programming I
= 6 + 1 = 7
7/31/2019 Day04 01 Variables
27/37
Overriding PrecedenceOverriding Precedence
VBs default operation order can be over-ridden easily!
In particular, operations enclosed byparenthesis are evaluated
first Examples:
Our example, stated as: z = 3 * (2 + 1) = 3 * 3 = 9
However, stated as: y = (3 * 2) + 1 = 6 + 1 = 7
Thus, parenthesis provide simpleprogram control, during execution
Parentheses inside of parentheses
The most internal operations are performed first Example: X = (((2 + 1) * 3) + ((7 + 6) 4)) * 5
= ((3 * 3) + (13 -4)) * 5
27
= 18 * 5= 90
7/31/2019 Day04 01 Variables
28/37
StrStr and Val Functionand Val Function
Anathematic operators an only work with numbers. But
The result of arithmetic operations is a number. But the
Text ro ert of a label control where we want to dis lathese results) is a string type
We need to convert strin to number and number to strin
Val Function
Convert string type variable to a numeric value
Str Function Convert numeric value to string
28Programming I
7/31/2019 Day04 01 Variables
29/37
ConclusionConclusion In this lecture, we have discussed:
Declaration and Use of Variables Declaring Constants
Common Data Types for mathematics Inte ers and Doubles
Mathematical Operations arithmetic operators
assi nment o erators
precedence
Algorithm Design
And Implemented two Programs, using Visual Studio .NET:A. Simple Calculator
B. Price Calculator
With the remainder of the lecture, you should practice: Try creating the programs yourself
29Programming I
7/31/2019 Day04 01 Variables
30/37
Pro ramPro ram Sim leSim le CalculatorCalculator Make a simple program to implement these
Step 01 Control Arrangements
Step 02 Setting Properties
30Programming I
7/31/2019 Day04 01 Variables
31/37
Sim le Calculator Contd.Sim le Calculator Contd.
Step 03 Add Codes for all 8 Buttons
31Programming I
7/31/2019 Day04 01 Variables
32/37
Sim le Calculator Contd.Sim le Calculator Contd.
Step 03 Add Codes for all 8 Buttons
32Programming I
7/31/2019 Day04 01 Variables
33/37
Sim le Calculator Contd.Sim le Calculator Contd.
Step 03 Add Codes for all 8 Buttons
33Programming I
7/31/2019 Day04 01 Variables
34/37
Sim le Calculator Contd.Sim le Calculator Contd.
Step 03 Add Codes for all 8 Buttons
34Programming I
7/31/2019 Day04 01 Variables
35/37
Sim le Calculator Contd.Sim le Calculator Contd.
35Programming I
7/31/2019 Day04 01 Variables
36/37
Programming I Microsoft Visual Studio 2008
DISCUSSION TIME
Programming I by Lanka Rodrigo
7/31/2019 Day04 01 Variables
37/37
Acknowled ementAcknowled ement
This note is prepared jointly with Dr. Nishantha Giguruwa r g na re erence, y r. o n ose
Lanka RodrigoLanka Rodrigo -- Programming IProgramming I