Download presentation
Presentation is loading. Please wait.
Published byRhoda Hines Modified over 9 years ago
1
Lecture Set 5 Control Structures Part D - Repetition with Loops
2
Slide 2 Objectives Write Do loops to execute statements repeatedly Write For loops to execute statements repeatedly Learn the differences between For and Do loops (and Do While and Do Until ) Lear how to use loop Exit and Continue This material should mostly review so it should go quickly – you have seen most of it already …
3
Slide 3 Introduction to Loops These structures are part of nearly every program written Repetition is the most powerful feature of any language – it is what computers do best (and FAST!) Learning how to construct loops with appropriate loop parameters can make difficult tasks seem easy (and coded with a short instruction sequence
4
Slide 4 Executing Statements Repeatedly Situations where repetition is used Calculating payroll – a loop is used to calculate the payroll for each employee Reading multiple records in a file – a loop is used to process each record in a file Graphical animations – a loop is used to move graphical objects about the screen Accounting problems – depreciation and loan balances are calculated repeatedly for multiple periods
5
Slide 5 Types of Loops Pre-test loops test a condition before executing the statements in a loop Post-test loops execute the loop's statements first, and then test the loop's condition
6
Slide 6 Do While and Do Until Loops (Syntax) Do While and Do Until loops execute statements repeatedly Do [While | Until] condition [statements] [Exit Do] [statements] Loop statements
7
Slide 7 Do While and Do Until Loops (Syntax, continued) Do While and Do Until loops test the condition before executing the statements in a loop The condition evaluates to a Boolean value The Do While form executes statements while the condition is True The Do Until form executes statements until the condition becomes True The Exit Do statement causes the Do loop to exit immediately statements following the loop execute
8
Slide 8 Execution Flow of a Do Until Loop
9
Slide 9 Execution Flow of a Do While Loop
10
Slide 10 Loops and Counters A counter is a variable whose value increases by a constant value each time through a loop The constant value used to increment the counter is typically 1 A counter takes the following general form: Counter = Counter + 1 Counter += 1
11
Slide 11 Loops and Counters (Example) Print the counting numbers 1 through 10 using a Do Until loop Dim Counter As Integer = 1 Do Until Counter > 10 Debug.WriteLine(Counter) Counter += 1 Loop
12
Slide 12 Loops and Counters (Example, continued) Print the counting numbers 1 through 10 using a Do While loop Dim Counter As Integer = 1 Do While Counter <= 10 Debug.WriteLine(Counter) Counter += 1 Loop
13
Slide 13 The Role of an Accumulator An accumulator is similar to a counter An accumulator is updated each time the statements in a loop execute It can be used to calculate (accumulate) a total An accumulator usually takes the following general form: Accumulator = Accumulator + value Accumulator += value But we will see other forms of “accumulators” as well – they are a powerful programming tool
14
Slide 14 Accumulator (Example) Store the sum of the counting numbers 1 through 10 Dim Counter As Integer = 1 Dim Accumulator As Integer = 0 Do While Counter <= 10 Debug.WriteLine(Counter) Accumulator += Counter Counter += 1 Loop
15
Slide 15 Infinite Loops A condition must ultimately occur causing a loop to exit Loops can be mistakenly written so that statements execute indefinitely These loops are called infinite loops Infinite loop example (Counter is always equal to 1): Dim Counter As Integer = 1 Do While Counter < 10 Debug.WriteLine(Counter) Loop
16
Slide 16 Nested Do Loops and Decision-making Loops can be nested, just as decision-making statements can be nested Loops can contain decision-making statements Decision-making statements can contain loops Both can contain nested sequence structures HOWEVER – it is generally a good idea NOT to nest loops inside loops or decisions. Put them in a separate procedure instead.
17
Slide 17 Combining Looping and Decision-making Statements (Example) Determine whether a number is prime Private Shared Function IsPrime( _ ByVal arg As Integer) As Boolean Dim Count As Integer = 2 Do While Count < arg If (arg Mod Count) = 0 Then Return False End If Count += 1 Loop Return True End Function
18
Slide 18 For Loops (Introduction) For loops execute statements repeatedly Use a For loop in place of a Do loop when the iteration count (the number of times the loop will execute) is known in advance For loops run more quickly than comparable Do loops For loops are more readable than equivalent Do loops
19
Slide 19 For Loops (Syntax) For counter = start To end [Step increment] [statements] [Exit For] [statements] Next [counter] statements
20
Slide 20 For Loops (Syntax, continued) The For and Next statements mark the beginning and end of a For loop counter is first initialized to start The value of counter is incremented automatically Do not explicitly set the value of counter By default, counter is incremented by one each time through the loop This value can be changed by including the Step increment clause
21
Slide 21 For Loops (Example) Print the counting numbers 1 to 10 Dim Counter As Integer For Counter = 1 To 10 Debug.WriteLine(Counter) Next Counter
22
Slide 22 Execution of a For Loop
23
Slide 23 Nested For Loop (Example (to be avoided) ) Print a multiplication table Dim Factor1, Factor2, Result As Integer For Factor1 = 1 To 10 For Factor2 = 1 To 10 Result = Factor1 * Factor2 Debug.Write(Result.ToString() & " ") Next Debug.WriteLine("") Next
24
Slide 24 Using the Step Keyword Use the Step keyword to modify a counter's value by a value other than 1 Example to decrement a counter by 5 Dim CurrentYear As Integer For CurrentYear = 2000 To 1900 Step –5 Debug.WriteLine(CurrentYear.ToString) Next CurrentYear
25
Slide 25 Exit and Continue Statements Can be used with any loop form Exit causes the innermost loop that contains it to exit immediately, sending control to the first instruction that follows the loop Continue causes the innermost loop that contains it to repeat immediately (effectively sending control to the bottom of the loop so that loop repetition can take place) The keywords Exit and Continue must be followed by keywords indicating the type of loop they are used in ( Exit For or Exit Do )
26
Slide 26 The Exit Statement
27
Slide 27 The Continue Statement
28
Slide 28 The Implications of Long-running Loops Some loops can run for hours Display messages periodically in long-running loops, or Show a progress bar as the loop is running so the user knows that progress is being made Improve the performance of long-running loops where possible Move unnecessary statements outside of a loop
29
Slide 29 Setting Breakpoints (A For loop with a breakpoint and an Execution Point)
30
Slide 30 Setting and Clearing Breakpoints
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.