Repetition
|
|
Versions All programming languages provide control structures that allow a statement or a block of statements to be repeated multiple times. Most provide three basic forms of looping constructs--pre-test loops, post-test loops, and count-controlled loops--and VB provides various implementations of each of these. Looping structures are designed such that the loop will continue until the value of some condition changes from True to False, or in some structures from False to True. Repetition can be implemented as:
|
|||||||||
|
|
|||||||||
|
In pre-test loops the condition is checked before the body of the loop is executed. VB provides three different implementations of the pre-test loop:
|
|||||||||
|
|
|||||||||
|
The While repetition structure allows the programmer to specify that an action is to be repeated based on the truth of some condition: While there are more items on my shopping list The action will be performed repeatedly while the condition remains True. Eventually, the condition will become False and the repetition terminates, transferring control to the first statement after the repetition structure.
Example: find the first power of 2 larger than 1000. Dim product As Integer While product <= 1000
|
|||||||||
|
|
|||||||||
|
Do While/Loop Repetition Structure The Do While/Loop behaves like the While repetition structure. Example: find the first power of 2 larger than 1000. Dim product As Integer Do While product <= 1000
The Do While/Loop figure is identical to the figure above. |
|||||||||
|
|
|||||||||
|
Do Until/Loop Repetition Structure The Do Until/Loop is a pre-test loop that tests a condition for falseness. Statements in the body of a Do Until/Loop are executed repeatedly as long as the loop-continuation test evaluates to False. The Do Until/Loop repetition structure can be used when it is more natural to express a condition "in the negative." The following figure illustrates the Do Until/Loop repetition structure.
example: Dim product As Integer Do Until product > 1000 |
|||||||||
|
|
|||||||||
|
In post-test loops the condition is checked after the body of the loop is executed. VB provides two different implementations of the post-test loop:
|
|||||||||
|
|
|||||||||
|
Do/Loop While Repetition Structure The Do/Loop While repetition structure tests the loop-continuation condition after the loop body is performed, thus executing the loop body at least once. The loop will continue as long as the condition remains True.
Note that the loop-continuation condition is not tested until after the action is performed at least once.
|
|||||||||
|
|
|||||||||
|
Do/Loop Until Repetition Structure The Do/Loop Until structure tests the loop-continuation condition after the loop body is performed; therefore, the loop body executes at least once. The loop will continue as long as the condition remains False.
|
|||||||||
|
|
|||||||||
|
Summary
|
|||||||||
|
|
|||||||||
|
Count-control loops require:
The loop terminates when the control variable exceeds 10 (i.e., counter becomes 12).
____________________________ The For/Next repetition structure handles all the details of count-controlled repetition. The previous procedure is rewritten as a For/Next loop below.
Special For/Next notes:
Note that the initialization occurs only once and that incrementing occurs each time after the body is executed.
Examples 1. Vary the control variable from 1 to 100 in increments of 1.
2. Vary the control variable from 100 to 1 in decrements of 1.
3. Vary the control variable from 7 to 77 in increments of 7.
4. Vary the control variable from 20 to 2 in decrements of 2.
5. Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17, 20.
6. Vary the control variable over the following sequence of values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0.
|
|||||||||
|
Avoid the use of of Exit statements to terminate loop execution.
Example of alternative (for tracing):
Const numGrades as Integer = 5 Dim counter As Integer = 0 Dim allGradesPassed As Boolean = True Dim grade As Integer
While (counter <= numGrades) And allGradesPassed grade = InputBox("Enter grade:") If (grade < 60) Then
allGradesPassed = False counter +=1 End While
If allGradesPassed Then MessageBox.Show("All students passed!", "Alert!", MessageBoxButtons.OK, MessageBoxIcon.Warning) EndIf
|