Visual Basic tutorial for everyone

Visual Basic 2013 Lesson 15: Looping

A procedure in a computer program that runs repeatedly until meeting the certain condition is called looping. A loop can go on repetitively as long as the processor and memory could support. For example, a program that adds a series of numbers until the sum exceeds a certain value or a program that prompts the user to enter data repeatedly until he or she enters the word ‘Finish’.

In Visual Basic 2013, there are three methods of Looping, the For…..Next loop, the Do loop, and the While…..End While loop. All methods produce the same repetitive effects.

15.1 Looping using For…Next Loop

The most common looping method in VB 2013 is the For….Next loop. The structure of a For…Next loop is as shown below:

For counter=startNumber to endNumber Step increment
One or more statements
Next

To exit a For…..Next Loop, you can place the Exit For statement within the loop; it is normally used together with the If….Then statement. For its application, you can refer to example 15.1 d.

Example 15.1 a

Dim counter as Integer
For counter=1 to 10
ListBox1.Items.Add (counter)
Next