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.

Slides:



Advertisements
Similar presentations
Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Advertisements

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 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.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
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 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.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
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.
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.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
INT213 Week 1.  A Named storage area (in RAM) that can hold a value (like a mailbox holding a letter)  Contents of a variable can be assigned, changed.
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.
Mark Dixon Page 1 03 – Dynamic HTML (client-side scripting)
Mark Dixon 1 14 – Functions and Modules. Mark Dixon 2 Questions: Parameters Consider the following code: Sub Move(ByRef obj, ByVal dist) obj.style.posLeft.
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.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 03 – Dynamic HTML (client-side scripting)
Mark Dixon Page 1 09 – Problem Solving. Mark Dixon Page 2 Admin: Test 1 Marked One-to-one debriefs, you can –Look at your script –Look at mark scheme.
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.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Conditional Execution.
15 – Structured Programming
12 – Object Oriented Programming
04 – Information Processing: Expressions, Operators & Functions
06 – Conditional Execution
Variables and Arithmetic Operations
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Introduction to TouchDevelop
08 – Iterative Execution.
3.2 Working with Data Scope of variables 29/07/2019.
Presentation transcript:

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 txtTax be after the following code has executed (txtSalary.value is 4589): If txtSalary.value < 5035 Then txtTax.value = 0 Else txtTax.value = txtSalary.Value * 0.20 End If true 0

Mark Dixon 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 is in or out of scope at a given point in a piece of code –Select a variable’s scope in your own program

Mark Dixon 4 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 10 –user enters a number –compare numbers and display appropriate message –Non-functional should be easy and fun to use

Mark Dixon 5 Example: GuessNum - Code Option Explicit Sub window_OnLoad() Randomize lblNum.innerText = Int(Rnd() * 10) End Sub Sub btnGuess_OnClick() If CInt(txtGuessNum.Value) = CInt(lblNum.innerText) Then lblResult.InnerText = "Correct" Else lblResult.InnerText = "Wrong, please try again" End If End Sub Generate Random Number between 0 and 9 Check user's answer against correct answer

Mark Dixon 6 Variables (why?) Variables useful for: –storing information you don't want user to see –reducing memory use –speed up execution –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 7 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 8 Variable declaration (how) Variables must be declared, using the following syntax (grammar): Dim identifier e.g. Dim weight Dim x Dim s Dim year represents the name of the variable

Mark Dixon 9 Variable assignment (how) Variables are assigned values, using the following syntax: identifier = expression e.g. x = 5 weight = name = "Bob" s = "Hello " Note: the data flows backwards (from right to left) read the = as 'becomes equal to'

Mark Dixon 10 Variables: Numeric Data

Mark Dixon 11 Variables: Dry running list the values of variables as each line is run: Dim num1 Dim num2 num1 = 8 num2 = num1 num1 = 3 num2 = 2 + num num1num2

Mark Dixon 12 Variables: String Data

Mark Dixon 13 Variables: String Manipulation

Mark Dixon 14 Dim d Dim f f = 3 d = f + 2 d = d + 4 df Questions: Dry running Produce a dry run table for the following code:

Mark Dixon 15 Example: GuessNum - Code Option Explicit Dim GuessNum Sub window_onLoad() Randomize GuessNum = Int(Rnd() * 10) 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 Create variable Put (Write) Random Number into variable Use (Read) variable

Mark Dixon 16 Example: Moon Orbit – Analysis SPECIFICATION User Requirements –need to keep children occupied/entertained, while learning about the moon's orbit Software Requirements –Functional: –Orbit of moon around earth should be animated –Children should be able to control speed and direction –Non-functional should be easy and fun to use

Mark Dixon 17 Problem solving: Pseudo-code To solve problem –think about how you would solve it manually (without computer) –think of steps you would take Moon position –increase angle –move moon horizontal position vertical position Convert to code 1 2 3

Mark Dixon 18 Trigonometry: In general angle (ang) hypotenuse (H) opposite (O) = Sin(ang) * H adjacent (A) = Cos(ang) * H

Mark Dixon 19 Trigonometry: Moon Orbit Sin(ang) * 150 Cos(ang) * 150 angle (ang) 150

Mark Dixon 20 Trigonometry: Radians Radians used by computers instead of degrees: 180 deg (3.1 rad) 90 deg (1.55 rad) 0 or 360 deg (0 or 6.2 rad) (4.65 rad) 270 deg rad = (deg/180) * 3.1 π π/2

Mark Dixon 21 Example: Moon Orbit v1.0 Moon orbit Angle: Sub Window_OnLoad() imgEarth.style.posLeft = document.body.clientwidth / 2 imgMoon.style.posLeft = imgEarth.style.posLeft txtAngle.value = 0 End Sub Sub btnCalc_OnClick() imgMoon.Style.posLeft = imgEarth.style.posLeft + (Sin(txtAngle.value) * 150) imgMoon.Style.posTop = imgEarth.style.posTop + (Cos(txtAngle.value) * 150) End Sub 1 2 3

Mark Dixon 22 Example: Moon Orbit v1.1 Use: –setInterval change angle move moon’s horizontal move moon’s vertical

Mark Dixon 23 Example: Moon Orbit v1.1 Moon orbit Sub window_onLoad() imgMoon.style.posLeft = imgEarth.style.posLeft imgMoon.style.posTop = imgEarth.style.posTop window.setInterval "MoonRotate()", 50 End Sub Sub MoonRotate() txtAngle.value = txtAngle.value imgMoon.style.posLeft = imgEarth.style.posLeft + (Sin(txtAngle.value) * 150) imgMoon.style.posTop = imgEarth.style.posTop + (Cos(txtAngle.value) * 150) End Sub

Mark Dixon 24 Moon orbit Sub window_onLoad() imgMoon.style.posLeft = imgEarth.style.posLeft imgMoon.style.posTop = imgEarth.style.posTop window.setInterval "MoonRotate()", 50 End Sub Sub MoonRotate() txtAngle.value = txtAngle.value imgMoon.style.posLeft = imgEarth.style.posLeft + (Sin( txtAngle.value ) * 150) imgMoon.style.posTop = imgEarth.style.posTop + (Cos( txtAngle.value ) * 150) End Sub Problem: Intermediate Results Intermediate result (angle) stored in object property (txtAngle.value) –verbose –visible –takes lot of memory

Mark Dixon 25 Moon orbit Option Explicit Dim ang Sub window_onLoad() imgMoon.style.posLeft = imgEarth.style.posLeft imgMoon.style.posTop = imgEarth.style.posTop window.setInterval "MoonRotate()", 50 ang = 0 End Sub Sub MoonRotate() ang = ang imgMoon.style.posLeft = imgEarth.style.posLeft + (Sin( ang ) * 150) imgMoon.style.posTop = imgEarth.style.posTop + (Cos( ang ) * 150) End Sub Example: Moon Orbit v1.2 Declaration of Variable Use of Variable shorter code invisible to user memory efficient faster execution initial value change value

Mark Dixon 26 Option Explicit: Variable undefined Must be first line of script Useful to force explicit variable declaration: Undeclared variables produce error message: Option Explicit Dim length length = 6 age = 5

Mark Dixon 27 Variables: Name redefined Option Explicit Dim x Dim y Dim x x = 23 y = = x can't use same name again

Mark Dixon 28 Variables: Expected statement Option Explicit Dim x Dim y x = 23 y = = x destination can't be literal

Mark Dixon 29 Example: Moon Orbit v1.3 How can we change the speed and direction of the moon?

Mark Dixon 30 Questions: Variable declaration Write a line of code that: –Declares a variable called x –Declares a variable called y –Declares a variable called surname Dim x Dim y Dim surname

Mark Dixon 31 Questions: Variable assignment Write a line of code that: –Assigns the value of 23 to the variable y –Puts 14.6 into a variable called x –Assigns the value of ‘John’ to the variable surname y = 23 x = 14.6 surname = "John"

Mark Dixon 32 Questions: Variable assignment 2 Write a line of code that: –Increases the value of x by 2.89 –Divides Km by 1.6 and puts the result in Miles x = x Miles = Km / 1.6

Mark Dixon 33 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 34 Variable Scope (what) Scope – accessibility/visibility –Local (declared within procedure) –Page (general declarations)

Mark Dixon 35 Variable Scope (How) Page 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

Mark Dixon 36 Variables: Scope (How)

Mark Dixon 37 Variable 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 38 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 defined error

Mark Dixon 39 Example: Ball Char (v2.5) Ball Char Sub window_onLoad() window.setInterval "MoveBallRight()", 50 End Sub Sub MoveBallRight() If (imgBall.style.posLeft imgBall.width) < (document.body.clientWidth) Then imgBall.style.posLeft = imgBall.style.posLeft + 5 Else window.setInterval "MoveBallLeft()", 50 End If End Sub Sub MoveBallLeft() If (imgBall.style.posLeft - 5) > 0 Then imgBall.style.posLeft = imgBall.style.posLeft - 5 Else window.setInterval "MoveBallRight()", 50 End If End Sub previous solution:  multiple timers  erratic behaviour

Mark Dixon 40 Example: Ball Char (v3) Dim hInc Sub window_onLoad() window.setInterval "BallMove()", 50 hInc = 5 End Sub Sub BallMove() Dim nxt nxt = imgBall.style.posLeft + hInc If nxt >= 0 And nxt + imgBall.width <= document.body.clientWidth Then imgBall.style.posLeft = nxt Else hInc = -hInc End If End Sub Using variables: shorter code invisible to user less memory faster execution page variable local variable

Mark Dixon 41 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 42 Variable Names Variables in same scope cannot have same name:

Mark Dixon 43 Tutorial Exercises: Guess Num LEARNING OBJECTIVE: use variables to simplify and make code more dynamic Task 1: Get GuessNum example working. You will need to create the html for the text box, button, and labels. Task 2: Modify GuessNum to tell the user whether their incorrect guess was higher of lower than the correct number. Task 3: Modify GuessNum to only allow 5 attempts before picking a new number.

Mark Dixon 44 Tutorial Exercises: Moon Orbit LEARNING OBJECTIVE: use variables to simplify and make code more dynamic Task 1: Get Moon Orbit examples working (v1 to v1.2). The code is provided on the slides. Task 2: Modify your page to allow the user to stop speed up and change the moon's direction (v1.3). Use the existing code as inspiration. Task 3: Modify your page so that it makes a water noise when the mouse moves over the Earth, and the ohh noise over the moon. Use code from previous lectures as inspiration. Task 4: Modify your page so that the diameter and mass of the Moon are displayed when the mouse moves over it. Do the same for the Earth. Go on-line to find the diameter and mass information.

Mark Dixon 45 Tutorial Exercises: Ball Char LEARNING OBJECTIVE: use variables to simplify and make code more dynamic Task 1: Get the Ball Char (v3) example working. Task 2: Add sound to the Ball Char (v3) example. Task 3: Get the Ball Char moving diagonally, bouncing off all four sides of the window. Task 4: Modify your page so that it allows the user to control how fast the ball character moves.