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.

Slides:



Advertisements
Similar presentations
Mark Dixon Page 1 12 – Functions and Modules. Mark Dixon Page 2 Questions: Parameters Name a parameter in the following code: Sub Move(ByRef obj, ByVal.
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 136Page 1 11 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –Revise Variables and Conditional execution.
Mark Dixon Page 1 02 – Dynamic HTML (client-side scripting)
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon Page 1 22 – Problem Solving. Mark Dixon Page 2 Session Aims & Objectives Aims –to provide a more explicit understanding of problem solving.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon Page 1 21 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon Page 1 02 – Queries: Query by Example.
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 Page 1 20 – Web applications: Writing data to Databases using ASP.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon Page 1 04 – Data Types. Mark Dixon Page 2 Admin: On-line Quiz.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
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 1 22 – Web applications: Writing data to Databases using ASP.Net.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
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 Page 1 10 – Procedures. Mark Dixon Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved in grouping instructions,
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 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 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.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
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.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
Mark Dixon Page 1 05 – Problem Solving & Data Types.
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.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
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 Tech – HTML. Mark Dixon Page 2 Admin Attendance Register: –log in to your profile.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon 1 9 – Case Study. Mark Dixon 2 Session Aims and Objectives Aims –To give an overview of the development of a web-page from initial idea to.
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 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
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 05 – Problem Solving & Data Types.
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.
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.
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.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
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.
08 – Iterative Execution.
11 – Functions and Modules
Presentation transcript:

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 code that: 1) reads the value of the variable called h 2) adds 1, and 3) puts the result back into h Dim h h = h + 1

Mark Dixon Page 3 Session Aims & Objectives Aims –To introduce the main concepts involved in getting the machine to perform repetitive tasks. Objectives, by end of this week’s sessions, you should be able to: –identify and correct errors in For and Do loops –create a For loop to repeat code a known number of times –create a Do loop to repeat code an unknown number of times

Mark Dixon Page 4 Example: Hello v0 Hello Option Explicit Sub btnHello_OnClick() parHello.innerHTML = parHello.innerHTML & "Hello " End Sub 1 user click: 1 Hello (1 line of code)

Mark Dixon Page 5 Example: Hello v1 Option Explicit Sub btnHello_OnClick() parHello.innerHTML = parHello.innerHTML & "Hello " End Sub 1 user click: 10 Hellos (10 lines of code) Lots of lines imagine 300 Hellos

Mark Dixon Page 6 Example: Hello v2 Option Explicit Sub btnHello_OnClick() Dim h h = 1 Do While h <= 10 parHello.innerHTML = parHello.innerHTML & "Hello " h = h + 1 Loop End Sub 1 user click: 10 Hellos (6 lines of code)

Mark Dixon Page 7 Hello v2: Do Loop variable h – used as counter

Mark Dixon Page 8 Example: Hello v3 Option Explicit Sub btnHello_OnClick() Dim h For h = 1 To 10 parHello.innerHTML = parHello.innerHTML & "Hello " Next End Sub 1 user click: 10 Hellos (4 lines of code)

Mark Dixon Page 9 Hello v3: For Loop variable h – set and incremented automatically

Mark Dixon Page 10 Advantages Less code: This makes program: –Easier to read –Easier to change (imagine 500 Hellos) Option Explicit Sub btnGo_OnClick() Dim h For h = 1 To 10 parHello.innerHTML = parHello.innerHTML & "Hello " Next End Sub Option Explicit Sub btnGo_OnClick() parHello.innerHTML = parHello.innerHTML & "Hello " End Sub Hello v1Hello v3 4 lines 10 lines

Mark Dixon Page 11 Do... Loop statement repeat code unknown number of times –more flexible than For –slower than For Syntax (Do While): Do While condition statementblock Loop Syntax (Do Until): Do Until condition statementblock Loop

Mark Dixon Page 12 For... Next statement repeat code known number of times –reduces length of code –easier to change Syntax: For variable = start To end statementblock Next

Mark Dixon Page 13 Example: Do … Loop Can do everything a For … Loop can: Dim iDim i i = 1 Do While i <= 10For i = 1 To 10 parN.innerText = i parN.innerText = i i = i + 1 LoopNext And more: Dim i i = 1 Do While i < 10 parN.innerText = i If (i / 2) = Int(i / 2) then i = i + 1 Else i = i + 3 End If Loop

Mark Dixon Page 14 Real Power of loops –using counter variable –do something slightly different each time Example: Option Explicit Dim num Dim tot tot = 0 For num = 1 To 4 tot = tot + num Next parRes.innerText = tot Example: Total

Mark Dixon Page 15 Example: Total

Mark Dixon Page 16 Example: Letter Count Option Explicit Sub btnCount_OnClick() Dim pos Dim last Dim char Dim count last = Len(txtWords.value) count = 0 For pos = 1 To last char = Mid(txtWords.value, pos, 1) If char = "e" Then count = count + 1 End If Next parCount.innerText = count End Sub

Mark Dixon Page 17 Letter Count 1)Start at first letter 2)If no more letters then go to 5 3)If letter is an e then add 1 to count 4)Go to 2 5)Display count

Mark Dixon Page 18 Question: For … Next What does the following code display in parNums: Dim s Dim counter s = "" For counter = 1 To 10 s = s & " " & counter Next parNums.innerText = s

Mark Dixon Page 19 Example: Pendulum v1 Pendulum Option Explicit Dim ang Dim speed Sub window_onload() imgMid.style.pixelLeft = document.body.clientWidth / 2 imgMid.style.pixelTop = document.body.clientHeight / 3 window.setInterval "Swing()", 25 ang = 0 speed = 0.04 End Sub Sub Swing() ang = ang + speed If ang > 0.5 Or ang < -0.5 Then speed = -speed End If imgPend.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 150 imgPend.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 150 End Sub

Mark Dixon Page 20 Example: Pendulum v2 … Sub Swing() ang = ang + speed If ang > 0.5 Or ang < -0.5 Then speed = -speed End If imgPend.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 150 imgPend.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 150 imgArm1.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 15 imgArm1.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 15 imgArm2.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 30 imgArm2.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 30 imgArm3.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 45 imgArm3.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 45 imgArm4.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 60 imgArm4.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 60 imgArm5.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 75 imgArm5.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 75 imgArm6.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 90 imgArm6.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 90 imgArm7.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 105 imgArm7.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 105 imgArm8.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 120 imgArm8.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 120 imgArm9.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 135 imgArm9.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 135 End Sub 56 lines of code

Mark Dixon Page 21 Example: Pendulum v3 … Sub Swing() Dim a Dim arm ang = ang + speed If ang > 0.5 Or ang < -0.5 Then speed = -speed End If imgPend.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * 150 imgPend.style.pixelTop = imgMid.style.pixelTop + cos(ang) * 150 For a = 1 To 9 Set arm = document.getElementById("imgArm" & a) arm.style.pixelLeft = imgMid.style.pixelLeft + sin(ang) * (a * 15) arm.style.pixelTop = imgMid.style.pixelTop + cos(ang) * (a * 15) Next End Sub 45 lines of code

Mark Dixon Page 22 Example: Graph Option Explicit Sub window_onload() Const xS = 10 ' Horizontal scaling factor. Const yS = 10 ' Vertical scaling factor. Dim xO ' Horizontal offset. Dim yO ' Vertical offset. Dim x ' Horizontal position. Dim y ' Vertical position. Dim h ' html data for plotted points. xO = 50 yO = document.body.clientHeight - 50 h = "" For x = 0 To 30 y = x h = h & "<img src='dot.gif' style='position: absolute;" h = h & "left: " & (xO + (x * xS)) & ";" h = h & "top: " & (yO - (y * yS)) & ";" h = h & "' />" Next document.body.innerHTML = h End Sub

Mark Dixon Page 23 Question: Do … Loop What does the following display in parNums: Dim s Dim num s = "" num = 10 Do While num > -6 s = s & " " & num num = num Loop parNums.innerText = s

Mark Dixon Page 24 Question: Do … Loop What does the following display in parNums: Dim num num = 6 Do Until num > 4 num = num + 5 parNums.innerText = parNums.innerText & num Loop nothing, 6 is already greater than 4

Mark Dixon Page 25 Loops: Errors Option Explicit Sub window_onLoad() For x = 1 To 10 Next End Sub

Mark Dixon Page 26 Loops: Errors Option Explicit Sub window_onLoad() Dim x For x = 1 To 10 End Sub Statement Expected (missing Next)

Mark Dixon Page 27 Loops: Errors Option Explicit Sub window_onLoad() Dim x Next End Sub

Mark Dixon Page 28 Loops: Errors Option Explicit Sub window_onLoad() Dim x For x = 1 To 10 Next End Sub

Mark Dixon Page 29 Tutorial Exercise: Hello LEARNING OBJECTIVE: use variables to make a for loop more flexible Task 1: Get the Hello Examples (0 to 2) working. Task 2: Modify your page so that it uses a variable to temporarily build to html. Task 3: Modify your page so that the user can control how many 'Hellos' appear.

Mark Dixon Page 30 Tutorial Exercise: Letter Count LEARNING OBJECTIVE: use a loop to search through a string (text) Task 1: Get the Letter Count Example (from the lecture) working. Task 2: Modify your Letter Count page, so that the user can control which letter is counted. Hint: You will need a text box for the user to type into. Task 3: Modify your code so that it responds immediately. Hint: Remove the button, and link your code to the KeyUp event of the text box. Task 3: Modify your Letter Count program, so that the user cannot type more than one letter in the letter text box. Hint: Use the len function.

Mark Dixon Page 31 Tutorial Exercise: Vowel Count LEARNING OBJECTIVE: build your own page from scratch, using a loop to search a string (piece of text) Task 1: Create a new page that counts the number of vowels (a, e, i, o, u) in a piece of text. Hint: similar to the letter count example.

Mark Dixon Page 32 Tutorial Exercise: Pendulum LEARNING OBJECTIVE: use a loop to shorten code responsible for visual display Task 1: Get the Pendulum examples (1 to 3) working. Task 2: Increase the number of dots for the arm. Task 3: Modify your code so that the arm and pendulum are centred correctly. hint: deduct half the width of the image.

Mark Dixon Page 33 Tutorial Exercise: Graphs LEARNING OBJECTIVE: use functions and operators to change the behaviour of code that uses a loop Task 1: Get the Graph example from the lecture working. Task 2: Modify the page so that it plots the graph: y = x / 2 Task 3: Create a new page that plots the graph: y = Sin(x) Hint: change the loop to use values of x from 0 to 6.2 in steps of 0.1 (VBScript pocket reference p. 85)