|
|
|
|
|
|
New to myExercises:
New to myNotes:
|
Posttest (bottom-driven loops) Types of Repetition Structures
The Repetition Structure simply means to repeat or to loop certain pieces of code. Every loop requires an evaluation or else it will result in an endless loop; at some point the condition must test false in order to exit the loop.
The condition is tested before entering the loop. The loop will never execute unless the condition tests true. In your code, the condition will appear before the code for the loop. Pretest loops are most commonly used. Pretest loops follow the True path.
Posttest (bottom-driven loops)
The loop is always executed one time. Then only will the condition be tested. Should the condition test true, the loop will execute a second time and so on. If the condition should test false, the loop will not execute for a second time. Posttest loops are used fairly seldom as it needs a specific reason to be used. Posstest loops follow the False path.
Types of Repetition Structures
Is a pre-test (top-driven) loop. Is used when you want the code to execute a precise number of times, e.g. to use the loop as a counter to execute a certain piece of code 5 times and then exit.
Syntax
Tips
Examples
Get three city names using an inputbox and display each city name and the value of the counter in a messagebox.
Dim intCount As Integer, strCity As String For intCount = 1 To 3 strCity = InputBox("Enter the city:", "City Entry") MessageBox.Show(strCity & " is city number " & intCount, "City", MessageBoxButtons.OK, _ MessageBoxIcon.Information Next intCount
Let the loop repeat until sngNumber has a value of 1 and display the results in the Output window of Visual Basic. The counter is shrunk from 2.5 to 1 in increments of -0.5
Dim sngNumber As Single For sngNumber = 2.5 To 1 Step -0.5 Debug.WriteLine(sngNumber) Next sngNumber
Move a picturebox around on your screen.
Dim intX As Integer For intX = 0 To 275 Step 5 Me.FruitPictureBox.SetBounds(intX, 0, 0, 0, BoundsSpecified.X) Next intX
Can be a pretest or posttest loop. Contains the keywords While or Until. Is used to repeat certain instructions while or until certain conditions are met.
Priming read is when you require user input before the loop starts and while the loop is running, you request user input again, usually you ask the same question in both cases. The purpose of priming read is that it sets up or prepares a loop, since this information is used when testing the loop condition. A priming read prevents an endless loop. See the examples below to see the practical use of priming read.
Syntax
Pretest Do...Loop
Posttest Do...Loop
Tips
Examples
Comparing a pretest and posttest Do...Loop to see the difference in results:
In this pretest loop example intCount is tested to see whether it is smaller or equal to 5. If True, the value of intCount is displayed in the output window and 1 is added to intCount before returning the loop to the top.
Dim intCount As Integer = 10 'declare counter Do While intCount <= 5 Debug.WriteLine(intCount) intCount = intCount + 1 'update counter Loop
In this posttest loop example, the current value of intCount, which is 1, is displayed in the Output window. Then 1 is added to intCount, which makes it's value 2, followed by the condition to test whether intCount is larger than 5, before returning to the top of the loop.
Dim intCount As Integer = 10 'declare counter Do Debug.WriteLine(intCount) intCount = intCount + 1 'update counter Loop Until intCount > 5
In the Output window the results will display quite different:
The pretest loop did not display a result because the condition was first tested and it tested false. The posttest loop displayed a result of 10 because it executed the statements first before testing the condition.
Example of using priming read in a Do...Loop:
Dim strSales As String Dim intNumSales As Integer 'declare counter Dim sngSumSales As Single 'declare accumulator Dim sngAverageSales As Single 'declare average
strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Sales Entry") 'priming read - get user input before commencing loop as this prepares the loop Do While strSales <> "" 'the condition uses the priming read information to execute the loop intNumSales = intNumSales + 1 'update counter sngSumSales = sngSumSales + Val(strSales) 'update accumulator strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Sales Entry") 'get user info while loop is running in order to satisfy the 'condition - otherwise loop will become endless Loop sngAverageSales = sngSumSales / intNumSales 'calculate average Me.AvgLabel.Text = Format(sngAverageSales, "currency")
*See flowchart in Zak, pg 377 (If you want this flowchart, send us an e-mail)
Using Exit...Do in a Do...Loop:
Don't confuse with For...Next loop. Used when you want the code to execute for each object in a collection.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
All content on this site is free for private use only | Contributions are encouraged | Thank you to Netfirms for the free hosting | If copyright content is published here or links to certain content violates some right/law, contact the Webmaster to have it removed immediately.
Site launched: October 2005 | Updated: July 20, 2006 | Link exchange | Site map | Contact us |