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

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Looping Structures: Do Loops
Mark Dixon Page 1 23 – Object Oriented Programming in PhP.
Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 22 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures.
Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 136Page 1 11 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –Revise Variables and Conditional execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging & Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon Page 1 13 – Object Oriented Programming.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Control Arrays & Container Controls.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Structured Query Language.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Chapter 6 - Visual Basic Schneider
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging, Sound, and Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon Page 1 10 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of code to declare a variable called h Write a line of.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Mark Dixon Page 1 09 – Arrays. Mark Dixon Page 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Repetition Chapter 7. Overview u For Loop u Do Loop  Do While Loop  Do Until Loop  Do Loop While  Do Loop Until u Nested Loops.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Dynamic HTML (client-side scripting)
Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures.
Mark Dixon SOFT 131Page 1 12 – Object Oriented Analysis, Design, and Programming.
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
Mark Dixon Page 1 15 – Structured Programming. Mark Dixon Page 2 Admin: Coursework 3 – Test In class test –9 Feb 2010 –2 Feb 2010: revision (technique)
1 Microsoft® Visual Basic®.NET Language # 2. 2 Flow-Control Statements If … End If Select Case … End Select For… Next Do … Loop Exit.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Procedures.
Mark Dixon 1 15 – Structured Programming. Mark Dixon 2 Admin: Test 2 In class test –11 Feb 2014 –4 Feb 2014: revision (technique) session 50 mins short.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Conditional Execution.
Selection Using IF THEN ELSE CASE Introducing Loops.
Identify the Appropriate Method for Handling Repetition
11 – Enumerated Data Types, & Procedure Parameters
6 – Iterative Execution.
Java for Beginners.
Think What will be the output?
Chapter (3) - Looping Questions.
Case & Repetitive Statements
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
The structure of programming
08 – Iterative Execution.
Gavin Restifo March 1, 2019 Today: Repetition Part 2 - For Loops
Presentation transcript:

Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration

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)

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

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

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

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

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

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

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

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

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

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 Next picCircles.Circle (2000, 2000), r Next picCircles.DrawMode = vbCopyPen End Sub Circles v3

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

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 Next picFace.Cls Next End Sub Face v3

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

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

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 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 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 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