View contents of this chapter

Iteration

Visual basic supports loops through a series of statements called looping statements. These statements can be used to implement repetitive statements.

Play Sound A loop is a series of one or more statements that execute more than one time.

For Loops

The For Loop executes a series of statements a specific number of times.

For counter = start To end [Step increment]
   [statements]
Next [counter]

Where:

  • counter is a numeric variable.
  • start is the initial value of counter.
  • end is the final value of counter.
  • step is optional. It is the amount counter is changed each time through the loop. If not specified, step defaults to one.
  • statements is optional part.

Consider the following program segment:

1: For counter = 1 To 5
2:   lblShow.Caption = counter
3: Next

The above loop repeats 5 times. The first time line "1:" is executed, counter is assigned 1. The body of the loop executes using that value. Line '3:" sends the loop to repeat again by incrementing counter by 1. The loop terminates when the value of counter becomes greater than 5.

Play Sound Use the Exit For statement if you want to terminate a loop before its normal termination.

 



© Universal Teacher Publications        INDEX Previous Screen Next Screen