08 iterative execution n.
Download
Skip this Video
Download Presentation
08 – Iterative Execution

Loading in 2 Seconds...

play fullscreen
1 / 20

08 – Iterative Execution - PowerPoint PPT Presentation


  • 104 Views
  • Uploaded on

08 – Iterative Execution. 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:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '08 – Iterative Execution' - quasim


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
session aims objectives
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:
    • To be able to implement code that does repetitive tasks, using looping structures:
      • known limits (for loop)
      • unknown limits (do loop)
example hello v0
Example: Hello v0
  • 1 user click: 1 Hello (1 line of code)

<html>

<head><title>Hello</title></head>

<body>

<input id=btnHello type=button value=Hello />

<p id=lblHello></p>

</body>

</html>

<script language=vbscript>

Option Explicit

Sub btnHello_OnClick()

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

End Sub

</script>

example hello v1
Example: Hello v1
  • 1 user click: 10 Hellos (10 lines of code)

Lots of lines imagine 300 Hellos

<script language=vbscript>

Option Explicit

Sub btnHello_OnClick()

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

End Sub

</script>

example hello v2
Example: Hello v2
  • 1 user click: 10 Hellos (4 lines of code)

<script language=vbscript>

Option Explicit

Sub btnHello_OnClick()

Dim h

For h = 1 to 10

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

Next

End Sub

</script>

for next statement
For ... Next statement
  • repeat code known number of times
    • reduces length of code
    • easier to change
  • Syntax:Forvariable=startToendstatementblockNext
advantages
Advantages
  • Less code:
  • This makes program:
    • Easier to read
    • Easier to change (imagine 500 Hellos)

Hello v1

Hello v2

Option Explicit

Sub btnGo_OnClick()

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

End Sub

Option Explicit

Sub btnGo_OnClick()

Dim h

For h = 1 To 10

lblHello.innerHTML = lblHello.innerHTML & "Hello<br>"

Next

End Sub

10lines

4 lines

example total
Example: Total
  • Real Power of loops
    • using counter variable
    • do something slightly different each time
  • Example: Dim num Dim tot tot = 0 For num = 1 To 5 tot = tot + num Next lblRes.InnerText = tot
question for next
Question: For … Next
  • What does the following code produce:

Dim counter

For counter = 1 To 10

lblNums.InnerText = lblNums.InnerText & counter

Next

  • What does the following code produce:

Dim i

For i = 24 To 8 Step -2

lblNums.InnerText = lblNums.InnerText & i & i * 2

Next

example letter count
Example: Letter Count

<script language=vbscript>

Option Explicit

Sub btnCount_OnClick()

Dim pos

Dim count

Dim char

count = 0

For pos = 1 To Len(txtWords.value)

char = Mid(txtWords.value, pos, 1)

If char = "e" Then

count = count + 1

End If

Next

lblCount.innerText = count

End Sub

</script>

example tones
Example: Tones

<script language="vbscript">

Option Explicit

Sub btnShow_OnClick()

Const stTag = "<span style=""background: #"

Dim h, p, msg, red

msg = txtMsg.value

h = ""

red = 255

For p = 1 To Len(msg)

h = h + stTag + Hex(red) + "0000"">"

h = h + Mid(msg, p, 1)

h = h + "</span>"

red = red - 5

Next

divTones.innerHTML = h

End Sub

</script>

do loop statement
Do ... Loop statement
  • repeat code unknown number of times
    • more flexible than For
    • slower than For
  • Syntax:Do [{While|Until} condition]

[statementblock]

Loop

example do loop
Example: Do … Loop
  • Can do everything a For … Loop can:Dim I Dim i i = 1 Do While i <= 10 For i = 1 To 10lblN.InnerText = ipicN.InnerText = i i = i + 1 Loop Next
  • And more:Dim i i = 1 Do While i < 10 lblN.innertext = i If (i / 2) = Int(i / 2) then i = i + 1 Else i = i + 3 End If Loop
question do loop
Question: Do … Loop
  • What does the following produce:

Dim num

num = 20

Do While num > -12

lblDo.InnerText = lblDo.InnerText & num

num = num - 1.5

Loop

  • What does the following produce:

Dim num

num = 6

Do Until num > 4

num = num + 5

lblDo.InnerText = lblDo.InnerText & num

Loop

tutorial exercise hello
Tutorial Exercise: Hello
  • Task 1: Get the Hello Example (from the lecture) 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.
tutorial exercise letter count
Tutorial Exercise: Letter Count
  • 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 a letter into.
  • 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 text box’s change event, and the len function.
tutorial exercise vowel count
Tutorial Exercise: Vowel Count
  • 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.
tutorial exercise tones
Tutorial Exercise: Tones
  • Task 1: Get the tones example from the lecture working.
  • Task 2: Modify the page so that it puts a space in between each letter.
  • Task 3: Change the program so that it uses shades of another colour instead.
  • Task 4: Create a new page that selects random shades of your selected colour. Hint: use the Rnd function.
tutorial exercise chess board
Tutorial Exercise: Chess Board
  • Task 1: Use one for loop inside another to create a chess board.
ad