Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration

2 Mark Dixon, SoCCE SOFT 131Page 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)

3 Mark Dixon, SoCCE SOFT 131Page 3 Repetitive User Action Option Explicit Private Sub btnGo_Click() picHello.Print "Hello" End Sub Hello v0

4 Mark Dixon, SoCCE SOFT 131Page 4 Example: Hello v1 Option Explicit Private Sub btnGo_Click() picHello.Print "Hello" End Sub Hello v1

5 Mark Dixon, SoCCE SOFT 131Page 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

6 Mark Dixon, SoCCE SOFT 131Page 6 Example: Hello v2 Option Explicit Private Sub btnGo_Click() Dim i As Long For i = 1 To 10 picHello.Print "Hello" Next End Sub Hello v2

7 Mark Dixon, SoCCE SOFT 131Page 7 Advantages Less code: This makes program: –Easier to read –Easier to change (50 Hellos) Option Explicit Private Sub btnGo_Click() Dim i As Long For i = 1 To 10 picHello.Print "Hello" Next End Sub Option Explicit Private Sub btnGo_Click() picHello.Print "Hello" End Sub Hello v1Hello v2 10 lines 4 lines

8 Mark Dixon, SoCCE SOFT 131Page 8 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 picMain.Print tot Example: Total Total

9 Mark Dixon, SoCCE SOFT 131Page 9 Example: Circles v1 Option Explicit Private Sub btnCircles_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 Mark Dixon, SoCCE SOFT 131Page 10 Example: Circles v2 Option Explicit Private Sub btnCircles_Click() Dim r As Long For r = 200 To 1600 Step 200 picCircles.Circle (2000, 2000), r Next End Sub Circles v2

11 Mark Dixon, SoCCE SOFT 131Page 11 Exercise: 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

12 Mark Dixon, SoCCE SOFT 131Page 12 Example: Circles v3 (Animation) Private Sub btnCircles_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

13 Mark Dixon, SoCCE SOFT 131Page 13 Example: 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

14 Mark Dixon, SoCCE SOFT 131Page 14 Example: 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

15 Mark Dixon, SoCCE SOFT 131Page 15 Do... Loop statement repeat code unknown number of times –more flexible than For –slower than For Syntax: Do [{While|Until} condition] [statementblock] Loop

16 Mark Dixon, SoCCE SOFT 131Page 16 Example: Do … Loop Can do everything a For … Loop can: Dim i As LongDim i As Long i = 1 Do While i <= 10For i = 1 To 10 picNums.Print i picNums.Print i i = i + 1 LoopNext 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

17 Mark Dixon, SoCCE SOFT 131Page 17 Exercise: 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

18 Mark Dixon, SoCCE SOFT 131Page 18 Example: 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 = 6000 Then inc = -inc times = times - 1 lblTimes.Caption = times lblTimes.Refresh End If picFace.Cls Loop End Sub Face v4


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved."

Similar presentations


Ads by Google