Download presentation
Presentation is loading. Please wait.
1
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables
2
Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –Introduce you to data storage concepts, i.e. constants, data types and variables Objectives, by end of this week’s sessions, you should be able to: –declare and use constants –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 136Page 3 Types of Information Numbers (numeric)29 (integer/whole) 56.23 (decimal/real) Text“Hello there!”“BOO” Pictures Sound
4
Mark Dixon, SoCCE SOFT 136Page 4 Constants similar to variable value given in declaration value can’t be changed useful for removing 'magic numbers' declaration syntax: name used to represent literal value [Global] Const constantname = expression
5
Mark Dixon, SoCCE SOFT 136Page 5 Example: CirCalc v1 Private Sub Form_Load() picCircle.ScaleMode = vbMillimeters End Sub Private Sub btnOK_Click() Dim radius As Single Dim circum As Single Dim area As Single radius = Val(txtRadius.Text) circum = 2 * 3.14159265359 * radius area = 3.14159265359 * (radius ^ 2) picCircle.Cls picCircle.Circle (30, 30), radius picInfo.Cls picInfo.Print "Circumference:" picInfo.Print " " & circum & "mm" picInfo.Print "Area:" picInfo.Print " " & area & "mm²" End Sub btnOK txtRadius picCircle picInfo
6
Mark Dixon, SoCCE SOFT 136Page 6 Example: CirCalc v2 Private Sub Form_Load() … Private Sub btnOK_Click() Const Pi = 3.14159265359 Dim radius As Single Dim circum As Single Dim area As Single radius = Val(txtRadius.Text) circum = 2 * Pi * radius area = Pi * (radius ^ 2) picCircle.Cls picCircle.Circle (30, 30), radius picInfo.Cls picInfo.Print "Circumference:" picInfo.Print " " & circum & "mm" picInfo.Print "Area:" picInfo.Print " " & area & "mm²" End Sub CirCalc btnOK txtRadius picCircle picInfo
7
Mark Dixon, SoCCE SOFT 136Page 7 Data Types Integer – whole numbers Long – whole numbers (large) Single – decimal numbers Double – decimal numbers (more precise) Currency – money String – text Boolean – True or False
8
Mark Dixon, SoCCE SOFT 136Page 8 Data Type Selection
9
Mark Dixon, SoCCE SOFT 136Page 9 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
10
Mark Dixon, SoCCE SOFT 136Page 10 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
11
Mark Dixon, SoCCE SOFT 136Page 11 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)
12
Mark Dixon, SoCCE SOFT 136Page 12 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
13
Mark Dixon, SoCCE SOFT 136Page 13 Numeric Variables
14
Mark Dixon, SoCCE SOFT 136Page 14 String (Text) Variables
15
Mark Dixon, SoCCE SOFT 136Page 15 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
16
Mark Dixon, SoCCE SOFT 136Page 16 Questions: 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
17
Mark Dixon, SoCCE SOFT 136Page 17 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)
18
Mark Dixon, SoCCE SOFT 136Page 18 Questions: 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
19
Mark Dixon, SoCCE SOFT 136Page 19 Example: 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
20
Mark Dixon, SoCCE SOFT 136Page 20 Example: GuessNum - Code 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 txtGuessNum btnGuess lblResult
21
Mark Dixon, SoCCE SOFT 136Page 21 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).
22
Mark Dixon, SoCCE SOFT 136Page 22 Questions: 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
23
Mark Dixon, SoCCE SOFT 136Page 23 Scope (what) Scope – accessibility/visibility –Local (declared within procedure) –Form/module/unit (general declarations)
24
Mark Dixon, SoCCE SOFT 136Page 24 Variable Scope (How) Module variables –general declarations (top) Local variables: –in procedures Option Explicit Dim mv as long Private Sub btnCalc_Click() Dim lv1 as long... End Sub Private Sub btnAdd_Click() Dim lv2 As Long... End Sub
25
Mark Dixon, SoCCE SOFT 136Page 25 Variable Scope
26
Mark Dixon, SoCCE SOFT 136Page 26 Scope (why) In short – Robustness of code/software –Protection from accidental outside interference One of many responses to code that is –Difficult to maintain, and –Unreliable –House of cards phenomenon Prevent: –Uncontrolled and ad hoc interactions between code Always define things at lowest level needed
27
Mark Dixon, SoCCE SOFT 136Page 27 Variable Scope: Errors In the following: Option Explicit Private Sub btnCalc_Click() Dim x As Integer x = 0 lblTotal.Caption = "£" & x End Sub Private Sub btnQuit_Click() x = 0 lblTotal.Caption = "£" & x End Sub Variable not defined error
28
Mark Dixon, SoCCE SOFT 136Page 28 Questions: Variable Scope Will this compile? Option Explicit … Dim x As integer … Private Sub thing() Dim z As Integer x = 23 y = "there" z = 12 end Private Sub btnTest_Click() Dim y As String y = "hello" x = 67 z = 53 End Sub Is x in scope? Is y in scope? Is z in scope? Is y in scope? Is x in scope? Is z in scope? Yes No Yes No
29
Mark Dixon, SoCCE SOFT 136Page 29 Example: Ball Char (v3) Option Explicit Private Sub tmrLeft_Timer() Me.picBallChar.Left = Me.picBallChar.Left - 100 If Me.picBallChar.Left < 0 Then tmrLeft.Enabled = False tmrRight.Enabled = True End If End Sub Private Sub tmrRight_Timer() Me.picBallChar.Left = Me.picBallChar.Left + 100 If Me.picBallChar.Left >= Me.ScaleWidth Then tmrRight.Enabled = False tmrLeft.Enabled = True End If End Sub
30
Mark Dixon, SoCCE SOFT 136Page 30 Example: Ball Char (v4) Option Explicit Dim xInc As Single Private Sub Form_Load() xInc = 100 End Sub Private Sub tmrMain_Timer() picBallChar.Left = picBallChar.Left + xInc If picBallChar.Left = frmBounce.ScaleWidth Then xInc = -xInc End If End Sub Variable (xInc) –reduces amount of code
31
Mark Dixon, SoCCE SOFT 136Page 31 Tutorial Exercise: GuessNum Task 1: Get GuessNum example working. Task 2: Modify GuessNum to tell the user whether their incorrect guess was higher or lower than the correct number. Task 3: Modify GuessNum to only allow 5 attempts before picking a new number.
32
Mark Dixon, SoCCE SOFT 136Page 32 Tutorial Exercise: Ball Char Task 1: Get Ball Char (v3) example working. Task 2: Add sound to Ball Char (v3) example Task 3: Get Ball Char moving diagonally, bouncing off all four sides of the window. Task 4: Modify your program so that it allows the user to control how fast the ball character moves.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.