Download presentation
Presentation is loading. Please wait.
1
Prepared By: Deborah Becker
This presentation will probably involve audience discussion, which will create action items. Use PowerPoint to keep track of these action items during your presentation In Slide Show, click on the right mouse button Select “Meeting Minder” Select the “Action Items” tab Type in action items as they come up Click OK to dismiss this box This will automatically create an Action Item slide at the end of your presentation with your points entered. Looping 4/25/2019 Prepared By: Deborah Becker
2
Prepared By: Deborah Becker
Topics of Discussion Do Loops For Next Nesting Looping Structures Visual Basic like other programming languages executes lines of code from the top of the program downwards. However, often a programmer does not wish all lines to be executed every time the program is run. Instead of writing several different programs for this job, branching and repeating commands can be used. Repeating Structures (or Loops) Branching commands are used to determine when to execute a small section of code and when not to run it. One example is the ‘If’ command. If something is true then the code will be run, if not it will not be executed and VB will continue further down the program. Repeating commands are useful for running small sections of code several times. If a programmer needs to read 200 lines of text from a file he could use 200 lines of code, one for every read statement, or alternatively a ‘For Next’ loop could do a similar job using just three or four lines! 4/25/2019 Prepared By: Deborah Becker
3
Prepared By: Deborah Becker
Looping Structures Do...Loop Until Do...Loop While Do While...Loop Do Until...Loop For…Next While…Wend The easiest way to understand repetition is with the aid of a practical example. Consider the steps a Chip Delivery Driver must perform as he delivers his chips to all the stores on his route. There are four basic steps: 1) check number of bags of chips requested by the store, 2) fetch required bags of chips from his truck, 3) carry back stales from store, and 4) travel to next store. If the route has 20 stores then the salesman has to carry out 80 individual steps to deliver all the chips. A computer simulation of this could be written in Visual Basic using 80 lines of code, one line for each step. However, this is very inefficient. A much better way is to ‘wrap’ the four basic steps in some kind of repeating structure. Visual Basic supports the following types: 4/25/2019 Prepared By: Deborah Becker
4
Information Flow in a Do Loop
l l l l l l l Do statement1 statement2 ... Loop 4/25/2019 Prepared By: Deborah Becker
5
Prepared By: Deborah Becker
Action of Do...Loop Until Condition False Condition True Do statement1 statement2 . . . Do statement1 statement2 . . . Loop Until condition Loop Until condition statement 4/25/2019 Prepared By: Deborah Becker
6
Prepared By: Deborah Becker
The 'Do ... Loop Until Dim intStoreNumber as Integer ‘Initialize variable intStoreNumber = 1 Do Count Number of Chips delivered Count Number of Stales Removed ‘Increment variable intStoreNumber intStoreNumber = intStoreNumber + 1 Loop until intStoreNumber > 20 This type of repeating structure will always be executed at least once because the loop exit statement is at the end. The Chip Delivery problem could be solved in the following way: intStoreNumber = 1 Do ' Check the order ' Fetch correct order ' Carry back stales intStoreNumber = intStoreNumber + 1 Loop Until intStoreNumber > 20 4/25/2019 Prepared By: Deborah Becker
7
Prepared By: Deborah Becker
?’s When is the condition checked? How many times will the….. Do Loop Until execute? 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 4/25/2019 Prepared By: Deborah Becker
8
Prepared By: Deborah Becker
Action of Do...Loop While Condition True Condition False Do statement1 statement2 . . . Do statement1 statement2 . . . Loop While condition statement Loop While condition 4/25/2019 Prepared By: Deborah Becker
9
Prepared By: Deborah Becker
?’s When is the condition checked? How many times will the….. Do While Loop execute? 4/25/2019 Prepared By: Deborah Becker
10
Prepared By: Deborah Becker
So the Do…Loop While…. is very similar to the previous ‘Do...Loop Until’. The difference is in the logic of the exit condition. For example, the ‘Loop While’ will repeat a block of instructions while a condition remains true, but the ‘Loop Until’ loop will exit when the exit condition turns true. A ‘Do...Loop While’ loop is very similar to the previous ‘Do...Loop Until’. The difference is in the logic of the exit condition. For example, the ‘Loop While’ will repeat a block of instructions while a condition remains true, but the ‘Loop Until’ loop will exit when the exit condition turns true. 4/25/2019 Prepared By: Deborah Becker
11
Action of Do While…Loop
Condition False Condition True Do While condition statement1 statement2 . . . Do While condition statement1 statement2 . . . Loop statement Loop 4/25/2019 Prepared By: Deborah Becker
12
Prepared By: Deborah Becker
Do While...Loop Do While...Loop (Will this loop ever execute?): Dim a as Integer a = 1 Do While a > 10 a = a - 1 Loop This loop is functionally equivalent to the ‘Do...Loop While’ except that the loop test condition is tested at the start of the loop. In practice this means that the ‘Do While...Loop’ might not be executed at all (if condition is false), whereas the ‘Do...Loop While’ will always be executed at least once. 4/25/2019 Prepared By: Deborah Becker
13
How Many Times Will This Do While…Loop Execute
Dim a as Byte a = 20 Do While a > 10 a = a - 1 Loop 20-1 = = 18 18-1 = = 16 16-1 = = 14 14-1 = = 12 12-1 = = 10 10-1 = = 8 8-1 = = 6 6-1 = = 4 4-1 = = 2 2-1 = = 0 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 = 10 4/25/2019 Prepared By: Deborah Becker
14
Prepared By: Deborah Becker
Action of Do Until Loop Condition False Condition True Do Until condition statement1 statement2 . . . Do Until condition statement1 statement2 . . . Loop Loop statement 4/25/2019 Prepared By: Deborah Becker
15
Prepared By: Deborah Becker
Do Until…Loop intStoreNumber = 1 Do Until intStoreNumber > 20 ‘Check the Order ‘Fetch the Order ‘Carry Back the Stales intStoreNumber = intStoreNumber + 1 Loop This loop, like the previous ‘Do While…Loop’ type, tests the condition at the beginning of the loop. However, whereas the previous loop repeats while the condition is true, the ‘Do Until…Loop’ repeats until the condition is true. 4/25/2019 Prepared By: Deborah Becker
16
Equivalent Do While and Do Until loops
Private Sub DoWhileTest_Click() Dim X As Integer Do While X < 3 MsgBox X X = X + 1 Loop End Sub Private Sub DoUntilTest_Click() Dim X As Integer Do Until X > = 3 MsgBox X X = X + 1 Loop End Sub 0, 1, 2 0, 1, 2 4/25/2019 Prepared By: Deborah Becker
17
Prepared By: Deborah Becker
The 'While ... Wend' CONTROL Executes a series of statements as long as the condition is True. Private Sub Form_Activate() Dim strPassword As String Const strSysPassW = help2003 While strPassword <> strSysPassW strPassword = InputBox("Please enter _ your Password:", “Password", "") Wend End Sub The ‘While...Wend’ loop is functionally equivalent to the ‘Do While...Loop’ discussed above. Once again this type of repeating structure should be used in situations where the loop will execute at least once. For example, a program which checks ban PIN numbers would always execute at least once: Note: In reality an InputBox would not be used since the typed PIN can be seen on the screen and also the correct PIN would not be held in a constant but instead retrieved from an encrypted file, but as an example it demonstrates the potential of ‘While...Wend’ loops. 4/25/2019 Prepared By: Deborah Becker
18
Prepared By: Deborah Becker
The Endless Program An infinite loop is a loop created where the condition will never be reached. 1.) Closing the Window or 2.) Pressing Ctrl-Break. *Make sure that the terminating condition can be reached………….. 4/25/2019 Prepared By: Deborah Becker
19
Prepared By: Deborah Becker
Terminating the Loops Do...Loop Until—repeats until the condition turns true Do...Loop While—repeats while condition remains true Do While...Loop—repeats while condition is true Do Until...Loop—repeats until the condition is true For...Next –used when the # of iterations is known Terminates when the upper limit of the loop is reached While...Wend—equivalent to Do While Should be used in situations where the loop must execute at least once 4/25/2019 Prepared By: Deborah Becker
20
Prepared By: Deborah Becker
Nesting loops Dim intCupsFilled , I as Integer intCupsFilled = 0 Do ‘Post condition loop ' boil water ' add tea bags to pot ' fill pot with boiling water For i = 1 to 5 ' pour tea into cup ' move to next cup intCupsFilled += 1 Next ‘(i is increased here) Loop Until intCupsFilled = 100 In the Delivery Man example above the delivery man performs the basic 4 steps at each of the 20 stores he serves. However, consider the more complex problem of simulating the pouring of cups of tea at a Church meeting. The problem is more complex because the tea pot holds only 5 cups of tea and there are 100 to serve. To solve this problem two loops can be used, one inside another (nested): Note: Different types of loops can be nested inside one another. In the above example the ‘For...Next’ loop is nested within a ‘Do...Loop Until’ structure. 4/25/2019 Prepared By: Deborah Becker
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.