06 Information Processing Datatypes Constants Variables Mark Dixon
06 – Information Processing: Data-types, Constants, Variables Mark Dixon, So. CCE SOFT 136 Page 1
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 Mark Dixon, So. CCE SOFT 136 Page 2
Types of Information • Numbers (numeric) 29 56. 23 • Text “Hello there!” “BOO” (integer/whole) (decimal/real) • Pictures • Sound Mark Dixon, So. CCE SOFT 136 Page 3
Constants • • • similar to variable value given in declaration value can’t be changed useful for removing 'magic numbers' declaration syntax: [Global] Const constantname = expression • name used to represent literal value Mark Dixon, So. CCE SOFT 136 Page 4
Example: Cir. Calc v 1 txt. Radius btn. OK pic. Circle pic. Info Mark Dixon, So. CCE SOFT 136 Private Sub Form_Load() pic. Circle. Scale. Mode = vb. Millimeters End Sub Private Sub btn. OK_Click() Dim radius As Single Dim circum As Single Dim area As Single radius = Val(txt. Radius. Text) circum = 2 * 3. 14159265359 * radius area = 3. 14159265359 * (radius ^ 2) pic. Circle. Cls pic. Circle (30, 30), radius pic. Info. Cls pic. Info. Print "Circumference: " Page 5
Example: Cir. Calc v 2 txt. Radius btn. OK Cir. Calc Private Sub Form_Load() … Private Sub btn. OK_Click() Const Pi = 3. 14159265359 Dim radius As Single Dim circum As Single Dim area As Single radius = Val(txt. Radius. Text) circum = 2 * Pi * radius area = Pi * (radius ^ 2) pic. Circle pic. Info Mark Dixon, So. CCE SOFT 136 pic. Circle. Cls pic. Circle (30, 30), radius pic. Info. Cls pic. Info. Print "Circumference: " pic. Info. Print " " & circum & "mm" pic. Info. Print "Area: " pic. Info. Print " " & area & "mm²" Page 6 End Sub
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 Mark Dixon, So. CCE SOFT 136 Page 7
Data Type Selection Mark Dixon, So. CCE SOFT 136 Page 8
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, So. CCE SOFT 136 Page 9
Example: Guess. Num - 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, So. CCE SOFT 136 Page 10
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, So. CCE SOFT 136 Page 11
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, So. CCE Memory SOFT 136 Type Page 12
Numeric Variables Mark Dixon, So. CCE SOFT 136 Page 13
String (Text) Variables Mark Dixon, So. CCE SOFT 136 Page 14
Variable declaration (how 1) • Variables must be declared, using the following syntax (grammar): Dim <identifier> As <type> e. g. Mark Dixon, So. CCE Dim Dim weight x s year SOFT 136 As As double long string long Page 15
Questions: 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, So. CCE SOFT 136 Page 16
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, So. CCE SOFT 136 Page 17
Questions: 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 = "John" – Assigns the value of 21 to the variable age = 21 Mark Dixon, So. CCE SOFT 136 Page 18
Example: Add. Num v 3 Add. Num Private Sub btn. Add_Click() Dim num 1 As Double Dim num 2 As Double Dim res As Double num 1 = Val(txt. Num 1. Text) num 2 = Val(txt. Num 2. Text) res = num 1 + num 2 lbl. Result. Caption = res End Sub • Variables used to: – spread code over several lines – makes code easier to understand Mark Dixon, So. CCE SOFT 136 Page 19
Example: Guess. Num - Code txt. Guess. Num Option Explicit Dim Guess. Num As Long Private Sub Form_Load() Randomize Guess. Num = Rnd() * 100 End Sub Private Sub btn. Guess_Click() If txt. Guess. Num. Text = Guess. Num Then btn. Guess lbl. Result. Caption = "Correct" Else lbl. Result. Caption = "Wrong, please try again" End If End Sub Mark Dixon, So. CCE SOFT 136 Page 20
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 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. OK (however x will be 3). Mark Dixon, So. CCE SOFT 136 Page 21
Questions: 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 = Km / 1. 6 – Joins two strings Surname and Forenames together, putting the result in Long. Name = Surname & Forenames Mark Dixon, So. CCE SOFT 136 Page 22
Scope (what) • Scope – accessibility/visibility – Local (declared within procedure) – Form/module/unit (general declarations) Mark Dixon, So. CCE SOFT 136 Page 23
Variable Scope (How) • Module variables Option Explicit Dim mv as long – general declarations (top) Private Sub btn. Calc_Click() Dim lv 1 as long • Local variables: . . . End Sub – in procedures Private Sub btn. Add_Click() Dim lv 2 As Long. . . End Sub Mark Dixon, So. CCE SOFT 136 Page 24
Variable Scope Mark Dixon, So. CCE SOFT 136 Page 25
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 Mark Dixon, So. CCE SOFT 136 Page 26
Variable Scope: Errors • In the following: Option Explicit Private Sub btn. Calc_Click() Dim x As Integer x = 0 lbl. Total. Caption = "£" & x End Sub Private Sub btn. Quit_Click() Variable not defined error x = 0 lbl. Total. Caption = "£" & x End Sub Mark Dixon, So. CCE SOFT 136 Page 27
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 btn. Test_Click() Dim y As String y = "hello" x = 67 z = 53 End Sub Mark Dixon, So. CCE SOFT 136 Is x in scope? Is y in scope? Is z in scope? Yes No Yes Is y in scope? Is x in scope? Is z in scope? Yes No Page 28
Example: Ball Char (v 3) Option Explicit Private Sub tmr. Left_Timer() Me. pic. Ball. Char. Left = Me. pic. Ball. Char. Left - 100 If Me. pic. Ball. Char. Left < 0 Then tmr. Left. Enabled = False tmr. Right. Enabled = True End If End Sub Private Sub tmr. Right_Timer() Me. pic. Ball. Char. Left = Me. pic. Ball. Char. Left + 100 If Me. pic. Ball. Char. Left >= Me. Scale. Width Then tmr. Right. Enabled = False tmr. Left. Enabled = True End If End Sub Mark Dixon, So. CCE SOFT 136 Page 29
Example: Ball Char (v 4) • Variable (x. Inc) – reduces amount of code Option Explicit Dim x. Inc As Single Private Sub Form_Load() x. Inc = 100 End Sub Private Sub tmr. Main_Timer() pic. Ball. Char. Left = pic. Ball. Char. Left + x. Inc If pic. Ball. Char. Left <= 0 Or pic. Ball. Char. Left >= frm. Bounce. Scale. Width Then x. Inc = -x. Inc End If End Sub Mark Dixon, So. CCE SOFT 136 Page 30
Tutorial Exercise: Guess. Num • Task 1: Get Guess. Num example working. • Task 2: Modify Guess. Num to tell the user whether their incorrect guess was higher or lower than the correct number. • Task 3: Modify Guess. Num to only allow 5 attempts before picking a new number. Mark Dixon, So. CCE SOFT 136 Page 31
Tutorial Exercise: Ball Char • Task 1: Get Ball Char (v 3) example working. • Task 2: Add sound to Ball Char (v 3) 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. Mark Dixon, So. CCE SOFT 136 Page 32
- Slides: 32