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.

Slides:



Advertisements
Similar presentations
Mark Dixon, SoCCE SOFT 131Page 1 22 – Object Oriented Analysis, Design, and Programming.
Advertisements

Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon Page 1 05 – Constants and Variables. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 mins short answer (5 - 6 words.
Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.
Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 02 – Dynamic HTML (client-side scripting)
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 13 – Object Oriented Programming.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Constants and Variables.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon Page 1 10 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of code to declare a variable called h Write a line of.
Mark Dixon Page 1 19 – Passing Data between pages: Forms, Sessions, & Query Strings.
CHAPTER THREE Representing Data: Constants and Variables.
Mark Dixon 1 07 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Mark Dixon Page 1 09 – Arrays. Mark Dixon Page 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
M Dixon 1 04 – Data Types & Debugging. Questions: Expressions a)What is the result of: 7 + Int(8.245) b)Write an expression to: put a random number into.
Mark Dixon 1 05 – Conditional Execution. Mark Dixon 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6 words) currently.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Dynamic HTML (client-side scripting)
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon Page 1 10 – Array Variables. Mark Dixon Page 2 C1 – In-class test Results mixed: –quite a few fails 34% (13/38) –some very good (91% max) ask.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 06 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.
Mark Dixon 1 08 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
University of Sunderland CIF 102/FIF102 Fundamentals of DatabasesUnit 15 Programming in Microsoft Access using VBA Using VBA to add functionality.
Mark Dixon Page 1 03 – Dynamic HTML (client-side scripting)
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon 1 03 – Information Processing. Mark Dixon 2 Questions: Events Consider the following code: a) How many unique events does it contain? b) Name.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
Mark Dixon 1 19 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Dynamic HTML (client-side scripting)
Mark Dixon Page 1 03 – Dynamic HTML (client-side scripting)
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, School of Computing SOFT 120Page 1 4. User Defined Functions (part 2)
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon SOFT 131Page 1 12 – Object Oriented Analysis, Design, and Programming.
Mark Dixon 1 06 – Conditional Execution. Mark Dixon 2 Admin: Test (next week) In class test –teaching week 7 50 minutes short answer (5 - 6 words max)
Mark Dixon Page 1 15 – Structured Programming. Mark Dixon Page 2 Admin: Coursework 3 – Test In class test –9 Feb 2010 –2 Feb 2010: revision (technique)
Mark Dixon Page 1 04 – Conditional Execution. Mark Dixon Page 2 Questions: Expressions a)What is the result of: 10 * Int( ) b)How many functions.
Mark Dixon Page 1 03 – Information Processing: Expressions, Operators & Functions.
CHAPTER THREE Representing Data: Constants and Variables.
Controlling Program Flow with Decision Structures.
Mark Dixon 1 15 – Structured Programming. Mark Dixon 2 Admin: Test 2 In class test –11 Feb 2014 –4 Feb 2014: revision (technique) session 50 mins short.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
Mark Dixon Page 1 03 – Information Processing: Expressions, Operators & Functions.
Using Forms and Form Elements In Visual Basic.NET.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Conditional Execution.
15 – Structured Programming
12 – Object Oriented Programming
04 – Information Processing: Expressions, Operators & Functions
6 – Iterative Execution.
Variables and Arithmetic Operations
Introduction to TouchDevelop
08 – Iterative Execution.
Presentation transcript:

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 mins multiple choice/short answer (5 - 6 words max) 25% of coursework mark

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

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

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

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

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)

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)

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

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

Mark Dixon, SoCCE SOFT 131Page 11 Variable assignment (how 2) Variables are assigned values, using the following syntax: = e.g. x = 5 weight = name = "Bob" s = "Hello " Note: the data flows backwards (from right to left) Numeric Variables String Variables

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

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

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

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 z = z - y Miles = Km / 1.6 LongName = Surname & Forenames

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)

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

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

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

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

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

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

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

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

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.