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:

Pre-test loops · While structure
· Do While/Loop structure
· Do Until/Loop structure
Post-test loops

· Do/Loop While structure
· Do/Loop Until
structure

Count-controlled loops · For/Next structure

Pre-Test Loops

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: 

  • In two of the versions the loop will execute as long as the condition remains True.  As soon as the condition becomes False, the loop will terminate.  If the condition is initially False, the loop will not be executed even once.  

  • In another version, the loop will execute as long as the condition remains False.  As soon as the condition becomes True, the loop will terminate.  If the condition is initially True, the loop will not be executed even once.  


While Repetition Structure

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
      Purchase next item and cross it off my 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.

Common Programming Error 

A common error is failing to include in the body of a While structure an action that eventually causes the condition in the While to become False. This will normally result in a repetition structure that will never terminate-an error caIled an "infinite loop."

Example: find the first power of 2 larger than 1000.

Dim product As Integer
product = 2

While product <= 1000
      product = product * 2
End While

  • A While repetition structure may have one or more statements in its body.

  • The keywords  End While terminate the While structure.

  • Execution continues with the next statement after the End While.

  • The figure below shows the While repetition structure.


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
product = 2

Do While product <= 1000
      product = product * 2
Loop

  • A Do While/Loop repetition structure may have one or more statements in its body.

  • The keyword Loop terminates the Do While/Loop structure.

  • Execution resumes with the next statement after the Do While/Loop.

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
product = 2

Do Until product > 1000
      product = product * 2
Loop


Post-test Loops

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: 

  • In one of the versions the loop will execute as long as the condition remains True.  As soon as the condition becomes False, the loop will terminate.  If the condition is initially False, the loop will be executed one time, because the condition is not checked until after the body of the loop.

  • In the other version, the loop will execute as long as the condition remains False.  As soon as the condition becomes True, the loop will terminate.  If the condition is initially True, the loop will be executed one time, because the condition is not checked until after the body of the 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.

Testing and Debugging Tip

Infinite loops are caused when the loop-continuation condition never becomes False. In a counter-controlled loop make sure the control variable is incremented (or decremented) in the body of the loop.  In a sentinel-controlled loop, make sure the sentinel value is eventually input.


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

Loop Type Tests for True Condition Tests for False Condition
Pre-test · While structure
· Do While/Loop structure
· Do Until/Loop structure
Post-test

· Do/Loop While structure

· Do/Loop Until structure
 

Count-Controlled Loops

Count-control loops require:

  1. The name of a loop control variable (or loop counter)

  2. The initial value of the control variable.

  3. The increment (or decrement) by which the control variable is modified each time through the loop.

  4. The condition that tests for the final value of the control variable (i.e., whether looping should continue).

The loop terminates when the control variable exceeds 10 (i.e., counter becomes 12).

Common Programming Error 

Because floating-point values may be approximate, controlling counting loops with floating point variables may result in imprecise counter values and inaccurate tests for termination

____________________________

For/Next Repetition Structure

The For/Next repetition structure handles all the details of count-controlled repetition.

The previous procedure is rewritten as a For/Next loop below.

  • The To keyword is required.

  • The loop continues as long as the control variable is less than or equal to the final value.

  • The optional Step keyword specifies the increment (the increment defaults to 1.)

  • The Step portion is typically omitted for increments of 1.

  • The loop control variable is incremented in the Next expression, which marks the end of the For/Next.

Good Programming Practice 

Including the For/Next's loop control variable name after the Next Statement is optional, but doing so improves program readability.

Special For/Next notes:

  • The starting value, ending value, and increment portions of a For/Next structure can contain arithmetic expressions. Example:
    For counter = x To 4 * x * y Step y \ x   is equivalent to the statement For counter = 2 To 80 Step 5

  • The Step portion of a For/Next structure may be negative, in which case it is a decrement and the loop actually counts downwards.

  • In nested For/Next loops, using the same control variable name for more than one loop is a syntax error.

  • If the loop-continuation condition is initially False (i.e., the starting value is greater than the ending value and the increment is positive), the For/Next's body is not performed.

Testing and Debugging Tip 

Although the value of the control variable can be changed in the body of a For/Next loop, avoid doing so because this practice can lead to subtle errors.  The number of times that the loop will execute is determined by the initial condition, and changing the value of the loop control variable will not affect that.

 

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.

For counter = 1 To 100

2. Vary the control variable from 100 to 1 in decrements of 1.

For counter = 100 To 1 Step -1

3.     Vary the control variable from 7 to 77 in increments of 7.

For counter = 7 To 77 Step 7

4.     Vary the control variable from 20 to 2 in decrements of 2.

For counter = 20 To 2 Step -2

5.     Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17, 20.

For counter = 2 To 20 Step 3

6.     Vary the control variable over the following sequence of values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0.

For counter = 99 To 0 Step -11


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
          MessageBox.Show("Failing grade encountered.", "Alert!", MessageBoxButtons.OK, MessageBoxIcon.
Exclamation)
     EndIf

     counter +=1

End While

 

If allGradesPassed Then

     MessageBox.Show("All students passed!", "Alert!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

EndIf