Upload
kenyon-craft
View
24
Download
1
Embed Size (px)
DESCRIPTION
06 – Iteration. Session Aims & Objectives. Aims Revise Variables and Conditional execution 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
06 – Iteration
Mark Dixon, SoCCE SOFT 131 Page 2
Session Aims & Objectives• Aims
– Revise Variables and Conditional execution– 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
Revision• Variables
• Conditions
Tax
Variables
Mark Dixon, SoCCE SOFT 131 Page 4
Example 1: Hello v1Option Explicit
Private Sub btnGo_Click() picHello.Print "Hello" picHello.Print "Hello" picHello.Print "Hello" picHello.Print "Hello" picHello.Print "Hello" picHello.Print "Hello" picHello.Print "Hello" picHello.Print "Hello" picHello.Print "Hello" picHello.Print "Hello"End Sub
Hello v1
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 [Step increment] [statementblock]Next
Mark Dixon, SoCCE SOFT 131 Page 6
• Advantages:– Less code (4 v 10)– Easier to read– Easier to change
Example 2: Hello v2Option Explicit
Private Sub btnGo_Click()Dim i As Long For i = 1 To 10 picHello.Print "Hello" NextEnd Sub
Hello v2
Mark Dixon, SoCCE SOFT 131 Page 7
Example 3: Total
• Real Power of loops– using counter variable
– do something slightly different each time
• Example: Dim num As LongDim tot As Longtot = 0For num = 1 To 5 tot = tot + numNext Total
Mark Dixon, SoCCE SOFT 131 Page 8
Exercise 1: For … Next• What does the following code produce:
Dim counter As Integer For counter = 1 To 10 picNums.Print counter Next
• What does the following code produce:Dim i As Integer For i = 24 To 8 Step -2 picNums.Print i, i * 2 Next
Loops
Mark Dixon, SoCCE SOFT 131 Page 9
Example 4: Circles v1
Option Explicit
Private Sub btnCicles_Click() picCircles.Circle (2000, 2000), 200 picCircles.Circle (2000, 2000), 400 picCircles.Circle (2000, 2000), 600 picCircles.Circle (2000, 2000), 800 picCircles.Circle (2000, 2000), 1000 picCircles.Circle (2000, 2000), 1200 picCircles.Circle (2000, 2000), 1400 picCircles.Circle (2000, 2000), 1600End Sub
Circles v1
Mark Dixon, SoCCE SOFT 131 Page 10
Example 5: Circles v2
Option Explicit
Private Sub btnCicles_Click()Dim r As Long For r = 200 To 1600 Step 200 picCircles.Circle (2000, 2000), r NextEnd Sub
Circles v2
Mark Dixon, SoCCE SOFT 131 Page 11
Example 6: Circles v3 (Animation)Private Sub btnCicles_Click()Dim r As LongDim p As Long picCircles.DrawMode = vbInvert For r = 200 To 1600 Step 200 picCircles.Circle (2000, 2000), r
For p = 1 To 5000000 Next
picCircles.Circle (2000, 2000), r Next picCircles.DrawMode = vbCopyPenEnd Sub
Circles v3
Mark Dixon, SoCCE SOFT 131 Page 12
Example 7: Letter CountOption Explicit
Private Sub btnCount_Click()Dim pos As LongDim count As LongDim char As String * 1 count = 0 For pos = 1 To Len(txtWords.Text) char = Mid$(txtWords.Text, pos, 1) If char = "e" Then count = count + 1 End If Next lblCount.Caption = countEnd Sub
Letter Count
Mark Dixon, SoCCE SOFT 131 Page 13
Example 8: Face v3Private Sub btnDraw_Click()Dim x As SingleDim p As Long picFace.FillStyle = vbSolid For x = 1000 To 6000 Step 50 picFace.FillColor = vbYellow picFace.Circle (x, 2400), 1000 picFace.Line (x, 2300)-Step(0, 400) picFace.FillColor = vbWhite picFace.Circle (x - 400, 2000), 250 picFace.Circle (x + 400, 2000), 250 picFace.FillColor = vbBlack picFace.Circle (x - 300, 2000), 40 picFace.Circle (x + 300, 2000), 40 picFace.Circle (x, 2500), 600, , 3.4, 6
For p = 1 To 500000 Next
picFace.Cls NextEnd Sub
Face v3
Mark Dixon, SoCCE SOFT 131 Page 14
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 15
Example 9: Do … Loop• Can do everything a For … Loop can:
Dim i As Long Dim i As Longi = 1Do While i <= 10 For i = 1 To 10 picNums.Print i picNums.Print i i = i + 1Loop Next
• And more:Dim i As Longi = 1Do While i < 10 picNums.Print I If (i / 2) = Int(i / 2) then i = i + 1 Else i = i + 3 End If Loop
Loops
Mark Dixon, SoCCE SOFT 131 Page 16
Exercise 2: Do … Loop• What does the following produce:
Dim num As Single num = 20 Do While num > -12 picDo.Print num num = num - 1.5 Loop
• What does the following produce:Dim num As Single num = 6 Do Until num > 4 num = num + 5 picDo.Print num Loop
Mark Dixon, SoCCE SOFT 131 Page 17
Example 10: Face v4Private Sub btnDraw_Click()Dim times As LongDim x As SingleDim p As LongDim inc As Long x = 1000 inc = 50 times = 6 Do Until times = 0 picFace.FillColor = vbYellow picFace.Circle (x, 2400), 1000 … … picFace.Circle (x, 2500), 600, , 3.4, 6
For p = 1 To 500000 Next
x = x + inc If x <= 1000 Or x >= 6000 Then inc = -inc times = times - 1 lblTimes.Caption = times lblTimes.Refresh End If
picFace.Cls LoopEnd Sub
Face v4