Transcript slides

05 – Information Processing:
Data-types, Variables,
Operators & Functions
Mark Dixon, SoCCE
SOFT 131
Page 1
Session Aims & Objectives
• Aims
– Introduce you to data storage concepts,
i.e. data types and variables
– Introduce you to processing concepts,
i.e. operators and functions
• Objectives,
by end of this week’s sessions, you should be able to:
– declare a variable, selecting appropriate data type
– assign a value to a variable,
• using combination of literal values, operators,
functions, and identifiers
Mark Dixon, SoCCE
SOFT 131
Page 2
Information Processing
• All computing problems:
– involve processing information/data
• information has meaning (e.g. 5lb 3.3kg 18 years)
• data has no meaning
(e.g 5 3.3 18)
– following this pattern:
Input Data
Process
Output Data
• For example:
– to multiply two numbers:
7
*
9
Mark Dixon, SoCCE
SOFT 131
7 * 9 = 63
63
Page 3
Information Processing (cont.)
• Hence, to solve any computing problem ask:
– what information goes in
– what processing is done to it
– what information comes out
Mark Dixon, SoCCE
SOFT 131
Page 4
Example: Multiply
Multiply
Option Explicit
Private Sub btnMultiply_Click()
lblResult.Caption = txtNum1.Text * txtNum2.Text
End Sub
Mark Dixon, SoCCE
SOFT 131
Page 5
Expressions: Evaluation, & Substitution
• The following assignment statement:
lblResult.Caption = txtNum1.Text * txtNum2.Text
contains an expression
• Given values for txtNum1.Text and txtNum2.Text
txtNum1.Text = "7", txtNum2.Text = "9"
can evaluate expression:
lblResult.Caption = txtNum1.Text * txtNum2.Text
lblResult.Caption = "7" * "9"
lblResult.Caption = 63
Mark Dixon, SoCCE
SOFT 131
(from above)
(substitute)
(calculate)
Page 6
Example: AddNum v1
AddNum
Option Explicit
Private Sub btnAdd_Click()
lblResult.Caption = txtNum1.Text + txtNum2.Text
End Sub
Mark Dixon, SoCCE
SOFT 131
Page 7
Functions & Operators
• Used to:
– process (manipulate) data
• Both Functions & Operators:
– take input data/parameters (1 or more item)
– process it
– return a result
• which replaces the expression (substitution)
Parameter(s)
Mark Dixon, SoCCE
Function
SOFT 131
Result
Page 8
Functions & Operators (cont.)
• Functions: come before the data (which is in brackets)
Sqr(16)
Abs(-23)
Int(2.543)
Val("63")
Left$("123",2)
square root function
absolute value function
integer function
value function
left string function
result is 4
result is 23
result is 2
result is 63
result is "12"
• Operators: sit between the data
5+2
5-2
5*2
5/2
"5" & "2"
Mark Dixon, SoCCE
addition operator
subtraction operator
multiplication operator
division operator
string concatenation
SOFT 131
result is 7
result is 3
result is 10
result is 2.5
result is "52"
Page 9
Exercise: Expressions
• What is the result of:
1 + Val("23") + Int(2.76786) + Sqr(Int(9.4523))
1 + 23 + 2 + 3 = 29
• What is the result of:
"23" & "18" + Left$("bob",1) + Right$("sal",2)
"23" & "18" & "b" & "al" = "2318bal"
• Write an expression to:
give integer value of "16.7658765"
Int(Val("16.7658765"))
• Write an expression to:
give the first two letters of "Mr John Smith"
Left$("Mr John Smith", 2)
Mark Dixon, SoCCE
SOFT 131
Page 10
Example: AddNum v2
AddNum
Option Explicit
Private Sub btnAdd_Click()
lblResult.Caption = Val(txtNum1.Text) + Val(txtNum2.Text)
End Sub
Mark Dixon, SoCCE
SOFT 131
Page 11
Types of Information
• Numbers (numeric)
29
56.23
• Text
“Hello there!” “BOO”
(integer/whole)
(decimal/real)
• Pictures
• Sound
Mark Dixon, SoCCE
SOFT 131
Page 12
Data Types
• Integer – whole numbers
• Long – whole numbers (large)
• Single – decimal numbers
• Double – decimal numbers (more precise)
• Currency – money
• String – text
Mark Dixon, SoCCE
SOFT 131
Page 13
Data Type Selection
String
text
Currency
What information
will it hold?
money
number
Mark Dixon, SoCCE
Will the number
ever have a
fraction?
yes
no
SOFT 131
Double
Will the number
ever need to be
very precise?
yes
no
Single
Will the number
ever be large?
yes
Long
no
Integer
Page 14
Data Storage
• Data can be stored in
– Controls
• visible to user (although can use visible property to hide)
• take lots of memory
• slow to access
– Variables
• Not visible to user
• take up very little memory
• fast to access
Mark Dixon, SoCCE
SOFT 131
Page 15
Example: GuessNum - Analysis
SPECIFICATION
• User Requirements
– need to keep children occupied/entertained, while
learning about maths
• Software Requirements
– Functional:
–computer picks a number between 0 and 100
–user enters a number
–compare numbers and display appropriate
message
– Non-functional
should be easy and fun to use
Mark Dixon, SoCCE
SOFT 131
Page 16
Variables (why?)
• Variables useful for:
– reducing memory use
– speed up execution
– storing information you don't want user to see
– storing intermediate results of calculations
temporarily (makes code easier to understand)
– making code easier to read (short variable name
instead of long object.property names)
Mark Dixon, SoCCE
SOFT 131
Page 17
Variables (what)
• Variables have
– Identifier (name) – you choose this, used to
refer to (reference) variable
– Type – you choose this (to suit purpose)
– Value – you set/change this
x 23
Integer
Name/Identifier
Value
Mark Dixon, SoCCE
Memory
SOFT 131
Type
Page 18
Variable declaration (how 1)
• Variables must be declared,
using the following syntax (grammar):
Dim <identifier> As <type>
e.g.
Mark Dixon, SoCCE
Dim
Dim
Dim
Dim
weight
x
s
year
SOFT 131
As
As
As
As
double
long
string
long
Page 19
Exercise: Variable declaration
• Write a line of code that:
– Declares a variable called x of type double
Dim x As double
– Declares a variable called y of type integer
Dim y As integer
– Declares a variable called surname of type string
Dim surname As string
– Declares a variable called age of type integer
Dim age As integer
Mark Dixon, SoCCE
SOFT 131
Page 20
Variable assignment (how 2)
• Variables are assigned values,
using the following syntax:
<identifier> = <expression>
e.g.
x
weight
name
s
=
=
=
=
5
109.45
"Bob"
"Hello "
Note: the data flows backwards (from right to left)
Mark Dixon, SoCCE
SOFT 131
Page 21
Exercise: Variable assignment
• Write a line of code that:
– Assigns the value of 23 to the variable y
y = 23
– Assigns the value of 14.6 to the variable x
x = 14.6
– Assigns the value of ‘John’ to the variable surname
surname = "John"
– Assigns the value of 21 to the variable age
age = 21
Mark Dixon, SoCCE
SOFT 131
Page 22
Demo
Mark Dixon, SoCCE
SOFT 131
Page 23
Example: AddNum v3
AddNum
Private Sub btnAdd_Click()
Dim num1 As Double
Dim num2 As Double
Dim res As Double
num1 = Val(txtNum1.Text)
num2 = Val(txtNum2.Text)
res = num1 + num2
lblResult.Caption = res
End Sub
• Variables used to:
– spread code over several lines
– makes code easier to understand
Mark Dixon, SoCCE
SOFT 131
Page 24
Example: GuessNum - Code
txtGuessNum
Option Explicit
Dim GuessNum As Long
Private Sub Form_Load()
Randomize
GuessNum = Rnd() * 100
End Sub
Private Sub btnGuess_Click()
If txtGuessNum.Text = GuessNum Then
lblResult.Caption = "Correct"
Else
lblResult.Caption = "Wrong, please try again"
End If
End Sub
Mark Dixon, SoCCE
SOFT 131
btnGuess
lblResult
Page 25
Variables: Errors
Option Explicit
Dim z as integer
OK, forces explicit variable declaration
OK
Sub Form_Click ()
Dim s As String
Dim x As Integer
Dim x As Integer
Print y
Print z
x = 40000
x = "21"
s = 21
x = 3.2
End Sub
OK
OK
OK
Duplicate definition error.
Variable not defined error.
OK, as z was declared at the form level.
Overflow error.
Type mismatch error.
Type mismatch error.
OK (however x will be 3).
Mark Dixon, SoCCE
SOFT 131
Page 26
Exercise: Variable assignment 2
• Write a line of code that:
– Increases the value of x by 2.89
x = x + 2.89
– Decreases the value of z by y
z = z - y
– Divides Km by 1.6 and puts the result in Miles
Miles = Km / 1.6
– Joins two strings Surname and Forenames
together, putting the result in LongName
LongName = Surname & Forenames
Mark Dixon, SoCCE
SOFT 131
Page 27