Date post: | 22-Dec-2015 |
Category: |
Documents |
View: | 225 times |
Download: | 0 times |
Mark Dixon, SoCCE SOFT 131 Page 1
05 – Information Processing:Data-types, Variables,Operators & Functions
Mark Dixon, SoCCE SOFT 131 Page 2
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 3
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:
• For example:– to multiply two numbers: 7 * 9 = 63
Input Data Process Output Data
9
763*
Mark Dixon, SoCCE SOFT 131 Page 4
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 5
Example: Multiply
Option Explicit
Private Sub btnMultiply_Click() lblResult.Caption = txtNum1.Text * txtNum2.TextEnd Sub
Multiply
Mark Dixon, SoCCE SOFT 131 Page 6
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
(from above) lblResult.Caption = "7" * "9" (substitute) lblResult.Caption = 63 (calculate)
Mark Dixon, SoCCE SOFT 131 Page 7
Example: AddNum v1
Option Explicit
Private Sub btnAdd_Click() lblResult.Caption = txtNum1.Text + txtNum2.TextEnd Sub
AddNum
Mark Dixon, SoCCE SOFT 131 Page 8
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)
FunctionParameter(s) Result
Mark Dixon, SoCCE SOFT 131 Page 9
Functions & Operators (cont.)• Functions: come before the data (which is in brackets)
Sqr(16) square root function result is 4 Abs(-23) absolute value function result is 23 Int(2.543) integer function result is 2 Val("63") value function result is 63 Left$("123",2) left string function result is "12"
• Operators: sit between the data
5 + 2 addition operator result is 7 5 - 2 subtraction operator result is 3 5 * 2 multiplication operator result is 10 5 / 2 division operator result is 2.5 "5" & "2" string concatenation result is "52"
Mark Dixon, SoCCE SOFT 131 Page 10
Exercise: Expressions• What is the result of:
1 + Val("23") + Int(2.76786) + Sqr(Int(9.4523))
• What is the result of:
"23" & "18" + Left$("bob",1) + Right$("sal",2)
• Write an expression to:
give integer value of "16.7658765"
• Write an expression to:
give the first two letters of "Mr John Smith"
1 + 23 + 2 + 3 = 29
"23" & "18" & "b" & "al" = "2318bal"
Int(Val("16.7658765"))
Left$("Mr John Smith", 2)
Mark Dixon, SoCCE SOFT 131 Page 11
Example: AddNum v2
Option Explicit
Private Sub btnAdd_Click() lblResult.Caption = Val(txtNum1.Text) + Val(txtNum2.Text)End Sub
AddNum
Mark Dixon, SoCCE SOFT 131 Page 12
Types of Information• Numbers (numeric) 29 (integer/whole)
56.23 (decimal/real)
• Text “Hello there!” “BOO”
• Pictures
• Sound
Mark Dixon, SoCCE SOFT 131 Page 13
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 14
Data Type Selection
Will the numberever have afraction?
Will the numberever be large?
number
What informationwill it hold?
String
Long
Integer
text
yes
no yes
no
Currencymoney Will the number
ever need to bevery precise?
Double
Single
yes
no
Mark Dixon, SoCCE SOFT 131 Page 15
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 16
Example: GuessNum - AnalysisSPECIFICATION
• 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 17
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 18
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
23x IntegerName/Identifier
Value TypeMemory
Mark Dixon, SoCCE SOFT 131 Page 19
Variable declaration (how 1)• Variables must be declared,
using the following syntax (grammar):
Dim <identifier> As <type>
e.g. Dim weight As doubleDim x As longDim s As stringDim year As long
Mark Dixon, SoCCE SOFT 131 Page 20
Exercise: Variable declaration
• Write a line of code that:
– Declares a variable called x of type double
– Declares a variable called y of type integer
– Declares a variable called surname of type string
– Declares a variable called age of type integer
Dim x As double
Dim y As integer
Dim surname As string
Dim age As integer
Mark Dixon, SoCCE SOFT 131 Page 21
Variable assignment (how 2)• Variables are assigned values,
using the following syntax:
<identifier> = <expression>
e.g. x = 5 weight = 109.45name = "Bob" s = "Hello "
Note: the data flows backwards (from right to left)
Mark Dixon, SoCCE SOFT 131 Page 22
Exercise: Variable assignment
• Write a line of code that:
– Assigns the value of 23 to the variable y
– Assigns the value of 14.6 to the variable x
– Assigns the value of ‘John’ to the variable surname
– Assigns the value of 21 to the variable age
y = 23
x = 14.6
surname = "John"
age = 21
Mark Dixon, SoCCE SOFT 131 Page 24
Example: AddNum v3Private Sub btnAdd_Click()Dim num1 As DoubleDim num2 As DoubleDim res As Double num1 = Val(txtNum1.Text) num2 = Val(txtNum2.Text) res = num1 + num2 lblResult.Caption = resEnd Sub
AddNum
• Variables used to:– spread code over several lines– makes code easier to understand
Mark Dixon, SoCCE SOFT 131 Page 25
Example: GuessNum - CodeOption ExplicitDim GuessNum As Long
Private Sub Form_Load() Randomize GuessNum = Rnd() * 100End Sub
Private Sub btnGuess_Click() If txtGuessNum.Text = GuessNum Then lblResult.Caption = "Correct" Else lblResult.Caption = "Wrong, please try again" End IfEnd Sub
txtGuessNum
btnGuess
lblResult
Mark Dixon, SoCCE SOFT 131 Page 26
Variables: ErrorsOption ExplicitDim z as integer
Sub Form_Click ()Dim s As StringDim x As IntegerDim x As Integer Print y Print z x = 40000 x = "21" s = 21 x = 3.2End Sub
OK, forces explicit variable declarationOK
OKOKOK 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 27
Exercise: Variable assignment 2• Write a line of code that:
– Increases the value of x by 2.89
– Decreases the value of z by y
– Divides Km by 1.6 and puts the result in Miles
– Joins two strings Surname and Forenames together, putting the result in LongName
x = x + 2.89
z = z - y
Miles = Km / 1.6
LongName = Surname & Forenames