Upload
abner
View
32
Download
0
Embed Size (px)
DESCRIPTION
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: - PowerPoint PPT Presentation
Citation preview
Mark Dixon, SoCCE SOFT 131 Page 1
08 – Iterative Execution
Mark Dixon, SoCCE SOFT 131 Page 2
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)
Mark Dixon, SoCCE SOFT 131 Page 3
Example: Hello v0
<html> <head> <title></title> <script language=vbscript> Option Explicit Sub btnHello_OnClick() lblHello.innerHTML = lblHello.innerHTML & "Hello<br>" End Sub </script> </head> <body> <p><input type=button id=btnHello value=Hello> <p id=lblHello> </body></html>
• 1 user click: 1 Hello (1 line of code)
Mark Dixon, SoCCE SOFT 131 Page 4
Example: Hello v1
<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>
• 1 user click: 10 Hellos (10 lines of code)
Mark Dixon, SoCCE SOFT 131 Page 5
For ... Next statement• repeat code known number of times
– reduces length of code
– easier to change
• Syntax:For counter = start To end [statementblock]Next
Mark Dixon, SoCCE SOFT 131 Page 6
Example: Hello v2
<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>
• 1 user click: 10 Hellos (4 lines of code)
Mark Dixon, SoCCE SOFT 131 Page 7
Advantages• Less code:
• This makes program:– Easier to read– Easier to change (imagine 50 Hellos)
Option Explicit
Sub btnGo_OnClick()Dim h For h = 1 To 10 lblHello.innerHTML = lblHello.innerHTML & "Hello<br>" NextEnd Sub
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
Hello v1 Hello v2
10lines
4 lines
Mark Dixon, SoCCE SOFT 131 Page 8
• Real Power of loops– using counter variable
– do something slightly different each time
• Example: Dim numDim tottot = 0For num = 1 To 5 tot = tot + numNextlblRes.InnerText = tot
Example: Total
Mark Dixon, SoCCE SOFT 131 Page 9
Example: Total
Mark Dixon, SoCCE SOFT 131 Page 10
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
Mark Dixon, SoCCE SOFT 131 Page 11
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>
Mark Dixon, SoCCE SOFT 131 Page 12
Example: Drinks v1 <script language=VBScript> Dim Units(6) Dim curUnit
Sub Window_OnLoad() curUnit = 0 End Sub
Sub btnAdd_OnClick() Units(curUnit) = txtUnit.value curUnit = curUnit + 1 End Sub
Sub btnClear_OnClick() Units(0) = 0 Units(1) = 0 Units(2) = 0 Units(3) = 0 Units(4) = 0 Units(5) = 0 Units(6) = 0 curUnit = 0 End Sub
Sub btnShow_OnClick() lblRes.innerText = "" lblRes.innerText = lblRes.innerText & Units(0) & " " lblRes.innerText = lblRes.innerText & Units(1) & " " lblRes.innerText = lblRes.innerText & Units(2) & " " lblRes.innerText = lblRes.innerText & Units(3) & " " lblRes.innerText = lblRes.innerText & Units(4) & " " lblRes.innerText = lblRes.innerText & Units(5) & " " lblRes.innerText = lblRes.innerText & Units(6) End Sub
….
Mark Dixon, SoCCE SOFT 131 Page 13
Example: Drinks v1 ….
Sub btnTotal_OnClick() Dim total total = 0 total = total + Units(0) total = total + Units(1) total = total + Units(2) total = total + Units(3) total = total + Units(4) total = total + Units(5) total = total + Units(6) lblRes.innerText = total End Sub
Sub btnFind_OnClick() If txtUnit.value = Units(0) Then lblRes.innerText = "Found in slot 0" ElseIf txtUnit.value = Units(1) Then lblRes.innerText = "Found in slot 1" ElseIf txtUnit.value = Units(2) Then lblRes.innerText = "Found in slot 2" ElseIf txtUnit.value = Units(3) Then lblRes.innerText = "Found in slot 3" ElseIf txtUnit.value = Units(4) Then lblRes.innerText = "Found in slot 4" ElseIf txtUnit.value = Units(5) Then lblRes.innerText = "Found in slot 5" ElseIf txtUnit.value = Units(6) Then lblRes.innerText = "Found in slot 6" Else lblRes.innerText = "Not Found" End If End Sub </script>
Mark Dixon, SoCCE SOFT 131 Page 14
Example: Drinks v2 <script language=VBScript> Dim Units(6) Dim curUnit
Sub Window_OnLoad() curUnit = 0 End Sub
Sub btnAdd_OnClick() Units(curUnit) = txtUnit.value curUnit = curUnit + 1 End Sub
Sub btnClear_OnClick() Dim i For i = 0 To 6 Units(i) = 0 Next curUnit = 0 End Sub
Sub btnShow_OnClick() Dim i lblRes.innerText = "" For i = 0 To 6 lblRes.innerText = lblRes.innerText & Units(i) & ", " Next End Sub
…
Mark Dixon, SoCCE SOFT 131 Page 15
Do ... Loop statement
• repeat code unknown number of times– more flexible than For
– slower than For
• Syntax:Do [{While|Until} condition] [statementblock]Loop
Mark Dixon, SoCCE SOFT 131 Page 16
Example: Do … Loop• Can do everything a For … Loop can:
Dim I Dim ii = 1Do While i <= 10 For i = 1 To 10 lblN.InnerText = i picN.InnerText =
i i = i + 1Loop Next
• And more:Dim ii = 1Do While i < 10 lblN.innertext = i If (i / 2) = Int(i / 2) then i = i + 1 Else i = i + 3 End If Loop
Mark Dixon, SoCCE SOFT 131 Page 17
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
Mark Dixon, SoCCE SOFT 131 Page 18
Tutorial Exercise• Task 1: Get the Hello Example (from the lecture)
working.• Task 2: Modify your page so that the user can
control how many 'Hellos' appear.• Task 3: Get the Letter Count Example (from the
lecture) working.• Task 4: 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 5: 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.
Mark Dixon, SoCCE SOFT 131 Page 19
Tutorial Exercise• Task 1: Get the Drinks v2 example (from the
lecture) working. You have the code for Add, Clear, & Show but not for Total and Find
• Task 2: Modify your page so that it displays a meaningful message when all elements of the array are used up.
Mark Dixon, SoCCE SOFT 131 Page 20
Tutorial Exercise• Task 1: Modify the German numbers page
(from last week) to prevent same random number appearing twice– store used numbers– use Do Until new value different from previous