Download presentation
Presentation is loading. Please wait.
1
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions
2
Mark Dixon, SoCCE SOFT 131Page 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
3
Mark Dixon, SoCCE SOFT 131Page 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 DataProcessOutput Data 9 7 63*
4
Mark Dixon, SoCCE SOFT 131Page 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
5
Mark Dixon, SoCCE SOFT 131Page 5 Example 1: Multiply Option Explicit Private Sub btnMultiply_Click() lblResult.Caption = txtNum1.Text * txtNum2.Text End Sub Multiply
6
Mark Dixon, SoCCE SOFT 131Page 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)
7
Mark Dixon, SoCCE SOFT 131Page 7 Example 2: AddNum v1 Option Explicit Private Sub btnAdd_Click() lblResult.Caption = txtNum1.Text + txtNum2.Text End Sub AddNum
8
Mark Dixon, SoCCE SOFT 131Page 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) Function Parameter(s) Result
9
Mark Dixon, SoCCE SOFT 131Page 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"
10
Mark Dixon, SoCCE SOFT 131Page 10 Exercise 1: 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)
11
Mark Dixon, SoCCE SOFT 131Page 11 Example 3: AddNum v2 Option Explicit Private Sub btnAdd_Click() lblResult.Caption = Val(txtNum1.Text) + Val(txtNum2.Text) End Sub AddNum
12
Mark Dixon, SoCCE SOFT 131Page 12 Types of Information Numbers (numeric)29 (integer/whole) 56.23 (decimal/real) Text“Hello there!”“BOO” Pictures Sound
13
Mark Dixon, SoCCE SOFT 131Page 13 Data Types Integer – whole numbers Long – whole numbers (large) Single – decimal numbers Double – decimal numbers (more precise) Currency – money String – text
14
Mark Dixon, SoCCE SOFT 131Page 14 Data Type Selection
15
Mark Dixon, SoCCE SOFT 131Page 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
16
Mark Dixon, SoCCE SOFT 131Page 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)
17
Mark Dixon, SoCCE SOFT 131Page 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 23xInteger Name/Identifier Value Type Memory
18
Mark Dixon, SoCCE SOFT 131Page 18 Variable declaration (how 1) Variables must be declared, using the following syntax (grammar): Dim As e.g. Dim weight As double Dim x As long Dim s As string Dim year As long
19
Mark Dixon, SoCCE SOFT 131Page 19 Exercise 2: 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
20
Mark Dixon, SoCCE SOFT 131Page 20 Variable assignment (how 2) Variables are assigned values, using the following syntax: = e.g. x = 5 weight = 109.45 name = "Bob" s = "Hello " Note: the data flows backwards (from right to left)
21
Mark Dixon, SoCCE SOFT 131Page 21 Exercise 3: 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
22
Mark Dixon, SoCCE SOFT 131Page 22 Example 4: AddNum v3 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 AddNum Variables used to: –spread code over several lines –makes code easier to understand
23
Mark Dixon, SoCCE SOFT 131Page 23 Variables: Errors Option Explicit Dim z as integer Sub Form_Click () Dim s As String Dim x As Integer Print y Print z x = 40000 x = "21" s = 21 x = 3.2 End Sub OK, forces explicit variable declaration OK Duplicate definition error. Variable not defined error. OK, as z was declared at the form level. Overflow error. Type mismatch error. OK (however x will be 3).
24
Mark Dixon, SoCCE SOFT 131Page 24 Exercise 5: 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.