1 / 17

# 06 – Iteration

06 – Iteration. Session Aims &amp; 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:

## 06 – Iteration

E N D

### Presentation Transcript

1. 06 – Iteration

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)

3. Revision • Variables • Conditions Variables Tax

4. Example 1: Hello v1 Hello v1 Option 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

5. For ... Next statement • repeat code known number of times • reduces length of code • easier to change • Syntax:Forcounter=startToend [Stepincrement] [statementblock]Next

6. Example 2: Hello v2 Hello v2 Option Explicit Private Sub btnGo_Click() Dim i As Long For i = 1 To 10 picHello.Print "Hello" Next End Sub • Advantages: • Less code (4 v 10) • Easier to read • Easier to change

7. Example 3: Total • Real Power of loops • using counter variable • do something slightly different each time • Example: Dim num As Long Dim tot As Long tot = 0 For num = 1 To 5 tot = tot + num Next Total

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

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), 1600 End Sub Circles v1

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 Next End Sub Circles v2

11. Example 6: Circles v3 (Animation) Private Sub btnCicles_Click() Dim r As Long Dim 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 = vbCopyPen End Sub Circles v3

12. Example 7: Letter Count Option Explicit Private Sub btnCount_Click() Dim pos As Long Dim count As Long Dim 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 = count End Sub Letter Count

13. Example 8: Face v3 Private Sub btnDraw_Click() Dim x As Single Dim 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 Next End Sub Face v3

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

15. Example 9: Do … Loop • Can do everything a For … Loop can:Dim i As LongDim i As Long i = 1 Do While i <= 10 For i = 1 To 10picNums.Print ipicNums.Print i i = i + 1 Loop Next • And more:Dim i As Long i = 1 Do While i < 10 picNums.Print I If (i / 2) = Int(i / 2) then i = i + 1 Else i = i + 3 End If Loop Loops

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

17. Example 10: Face v4 Private Sub btnDraw_Click() Dim times As Long Dim x As Single Dim p As Long Dim 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 Loop End Sub Face v4

More Related