mark dixon, socce soft 136page 1 06 – information processing: data-types, constants, variables
Post on 21-Dec-2015
225 views
TRANSCRIPT
![Page 1: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/1.jpg)
Mark Dixon, SoCCE SOFT 136 Page 1
06 – Information Processing:Data-types, Constants, Variables
![Page 2: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/2.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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
![Page 3: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/3.jpg)
Mark Dixon, SoCCE SOFT 136 Page 3
Types of Information• Numbers (numeric) 29 (integer/whole)
56.23 (decimal/real)
• Text “Hello there!” “BOO”
• Pictures
• Sound
![Page 4: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/4.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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
![Page 5: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/5.jpg)
Mark Dixon, SoCCE SOFT 136 Page 5
Example: CirCalc v1Private Sub Form_Load() picCircle.ScaleMode = vbMillimetersEnd Sub
Private Sub btnOK_Click()Dim radius As SingleDim circum As SingleDim 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
![Page 6: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/6.jpg)
Mark Dixon, SoCCE SOFT 136 Page 6
Example: CirCalc v2Private Sub Form_Load() …
Private Sub btnOK_Click()Const Pi = 3.14159265359Dim radius As SingleDim circum As SingleDim 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
![Page 7: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/7.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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
![Page 8: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/8.jpg)
Mark Dixon, SoCCE SOFT 136 Page 8
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
![Page 9: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/9.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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
![Page 10: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/10.jpg)
Mark Dixon, SoCCE SOFT 136 Page 10
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
![Page 11: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/11.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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)
![Page 12: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/12.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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
23x IntegerName/Identifier
Value TypeMemory
![Page 13: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/13.jpg)
Mark Dixon, SoCCE SOFT 136 Page 13
Numeric Variables
![Page 14: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/14.jpg)
Mark Dixon, SoCCE SOFT 136 Page 14
String (Text) Variables
![Page 15: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/15.jpg)
Mark Dixon, SoCCE SOFT 136 Page 15
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
![Page 16: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/16.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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
![Page 17: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/17.jpg)
Mark Dixon, SoCCE SOFT 136 Page 17
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)
![Page 18: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/18.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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
![Page 19: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/19.jpg)
Mark Dixon, SoCCE SOFT 136 Page 19
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
![Page 20: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/20.jpg)
Mark Dixon, SoCCE SOFT 136 Page 20
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
![Page 21: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/21.jpg)
Mark Dixon, SoCCE SOFT 136 Page 21
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).
![Page 22: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/22.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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
![Page 23: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/23.jpg)
Mark Dixon, SoCCE SOFT 136 Page 23
Scope (what)
• Scope – accessibility/visibility
– Local (declared within procedure)
– Form/module/unit (general declarations)
![Page 24: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/24.jpg)
Mark Dixon, SoCCE SOFT 136 Page 24
Variable Scope (How)
• Module variables– general
declarations (top)
• Local variables:– in procedures
Option ExplicitDim mv as long
Private Sub btnCalc_Click()Dim lv1 as long ...End Sub
Private Sub btnAdd_Click()Dim lv2 As Long ...End Sub
![Page 25: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/25.jpg)
Mark Dixon, SoCCE SOFT 136 Page 25
Variable Scope
![Page 26: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/26.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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
![Page 27: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/27.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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
![Page 28: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/28.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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?
YesNoYes
YesYesNo
![Page 29: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/29.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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 IfEnd 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 IfEnd Sub
![Page 30: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/30.jpg)
Mark Dixon, SoCCE SOFT 136 Page 30
Example: Ball Char (v4)
Option ExplicitDim xInc As Single
Private Sub Form_Load() xInc = 100End Sub
Private Sub tmrMain_Timer() picBallChar.Left = picBallChar.Left + xInc If picBallChar.Left <= 0 Or picBallChar.Left >= frmBounce.ScaleWidth Then xInc = -xInc End IfEnd Sub
• Variable (xInc)– reduces amount of code
![Page 31: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/31.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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.
![Page 32: Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables](https://reader030.vdocuments.net/reader030/viewer/2022032704/56649d6a5503460f94a48852/html5/thumbnails/32.jpg)
Mark Dixon, SoCCE SOFT 136 Page 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.