Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables

2 Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week 14 50 mins multiple choice/short answer (5 - 6 words max) 25% of coursework mark

3 Mark Dixon, SoCCE SOFT 131Page 3 Session Aims & Objectives Aims –Introduce you to (invisible) data storage concepts, i.e. variables Objectives, by end of this week’s sessions, you should be able to: –declare a variable –assign a value to a variable, using combination of literal values, operators, functions, and identifiers –Determine whether a variable or procedure is in or out of scope at a given point in a piece of code –Select a variable’s scope in their own program

4 Mark Dixon, SoCCE SOFT 131Page 4 InterDev Colour coding Uni Software – (M) Microsoft Visual Studio 6.0

5 Mark Dixon, SoCCE SOFT 131Page 5 Example: GuessNum SPECIFICATION User Requirements –need to keep children occupied/entertained, while learning about maths Software Requirements –Functional: –computer picks a number between 0 and 10 –user enters a number –compare numbers and display appropriate message –Non-functional should be easy and fun to use

6 Mark Dixon, SoCCE SOFT 131Page 6 Example: Random Sub Window_OnLoad() Randomize End Sub Sub btnRandom_OnClick() lblRes.innerText = Rnd() * 5 End Sub Random number generator: Shuffles random number generator Picks random number between 0 and 1

7 Mark Dixon, SoCCE SOFT 131Page 7 Data Storage Data can be stored in –Controls (e.g. text boxes, buttons, paragraphs) visible to user (although can use visible property to hide) take lots of memory slow to access open to all parts of page –Variables Not visible to user take up very little memory fast to access access can be restricted (scope)

8 Mark Dixon, SoCCE SOFT 131Page 8 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, & prevents need to re-calculate –making code easier to read (short variable name instead of long object.property names)

9 Mark Dixon, SoCCE SOFT 131Page 9 Variables (what) Variables have –Identifier (name) – you choose this, used to refer to (reference) variable –Value – you set/change this 23x Name/Identifier ValueMemory

10 Mark Dixon, SoCCE SOFT 131Page 10 Variable declaration (how 1) Variables must be declared, using the following syntax (grammar): Dim e.g. Dim weight Dim x Dim s Dim year represents the name of the variable

11 Mark Dixon, SoCCE SOFT 131Page 11 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) Numeric Variables String Variables

12 Mark Dixon, SoCCE SOFT 131Page 12 Option Explicit Useful to force explicit variable declaration: Option Explicit must be first line of script undeclared variables produce error message

13 Mark Dixon, SoCCE SOFT 131Page 13 Questions: Variable declaration Write a line of code that: –Declares a variable called x –Declares a variable called y –Declares a variable called surname –Declares a variable called age Dim x Dim y Dim surname Dim age

14 Mark Dixon, SoCCE SOFT 131Page 14 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

15 Mark Dixon, SoCCE SOFT 131Page 15 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

16 Mark Dixon, SoCCE SOFT 131Page 16 Example: GuessNum - Code Option Explicit Dim GuessNum Sub window_OnLoad() Randomize GuessNum = Int(Rnd() * 10) lblResult.innerText = GuessNum End Sub Sub btnGuess_OnClick() If CInt(txtGuessNum.Value) = GuessNum Then lblResult.InnerText = "Correct" Else lblResult.InnerText = "Wrong, please try again" End If End Sub I am thinking of a number between 0 and 10 Please guess the number Generate Random Number between 0 and 10 Temporary line (helps us test)

17 Mark Dixon, SoCCE SOFT 131Page 17 Variables: Errors Option Explicit Dim z Sub Window_OnClick() Dim s Dim x y = 5 z = 5 End Sub OK, explicit variable declaration OK Duplicate definition error. Variable not defined error. OK, as z is page level

18 Mark Dixon, SoCCE SOFT 131Page 18 Scope (what) Scope – accessibility/visibility –Local (declared within procedure) –Page (general declarations)

19 Mark Dixon, SoCCE SOFT 131Page 19 Variable Scope (How) Module variables –general declarations (top) Local variables: –in procedures Option Explicit Dim mv Sub btnCalc_OnClick() Dim lv1... End Sub Sub btnAdd_OnClick() Dim lv2... End Sub Scope Animation

20 Mark Dixon, SoCCE SOFT 131Page 20 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

21 Mark Dixon, SoCCE SOFT 131Page 21 Variable Scope Errors Spot the error in the following: Option Explicit Sub btnCalc_OnClick() Dim x x = 0 lblTotal.InnerText = "£" & x End Sub Sub btnQuit_OnClick() x = 0 lblTotal.InnerText = "£" & x End Sub Variable not found error

22 Mark Dixon, SoCCE SOFT 131Page 22 Question: Variable Scope Will this compile? Option Explicit Dim v Dim x … Sub Window_OnLoad() Dim z x = 23 y = "there" z = 12 end Sub btnTest_OnClick() Dim y 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

23 Mark Dixon, SoCCE SOFT 131Page 23 Example: Ball Char (v2.5) Test Sub Window_OnLoad() Window. SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight () If (picBall.hspace + 5) < (document.body.clientwidth - picBall.Width) Then picBall.hspace = picBall.hspace + 5 Else Window. SetInterval "MoveBallLeft", 50 End If End Sub Sub MoveBallLeft () If (picBall.hspace) > 0 Then picBall.hspace = picBall.hspace - 5 Else Window. SetInterval "MoveBallRight", 50 End If End Sub

24 Mark Dixon, SoCCE SOFT 131Page 24 Example: Ball Char (v3) Dim hInc Sub window_OnLoad() window. setInterval "BallMove", 50 hInc = 5 End Sub Sub BallMove () Dim nxt nxt = imgBall.hspace + hInc If nxt >= 0 And nxt + imgBall.width =< document.body.clientwidth Then imgBall.hspace = nxt Else hInc = -hInc End If End Sub

25 Mark Dixon, SoCCE SOFT 131Page 25 Tutorial Exercises Task 1: Get GuessNum example working. Task 2: Modify GuessNum to only allow 5 attempts before picking a new number. Task 3: Get Ball Char (v3) example working. Task 4: Add sound to Ball Char (v3) example Task 5: Get Ball Char moving diagonally, bouncing off all four sides of the window.


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week."

Similar presentations


Ads by Google