Lesson 15

Looping

In Visual Basic 2015, looping is a process where a block of statements runs repeatedly until a specified condition is met. Looping is extremely useful when you need to repeat calculations, display sequences, collect repeated input, or process lists of values without writing the same statements again and again.

Lesson focus:

There are three major loop structures in Visual Basic 2015: For...Next, Do...Loop, and While...End While. Each has a different style and is useful in different situations.

Lesson Overview

Lesson15
TopicLooping
Main FocusRepeating Statements
Key StructuresFor, Do, While
Next StepSub Procedures
15.1 For...Next
15.2 Do...Loop
15.3 While...End While
15.4 Loop Control

Why Looping Matters

Without loops, a programmer would need to write the same statements repeatedly. This would make code long, repetitive, and difficult to maintain. A loop lets the computer repeat a task automatically.

For example, you can use loops to:

  • Add numbers repeatedly until a total exceeds a chosen value.
  • Display a sequence such as 1 to 10 in a list box.
  • Process repeated user input.
  • Calculate a cumulative sum or running total.

The For...Next Loop

The For...Next loop is used when you know in advance how many times the loop should run. It uses a counter variable that starts at one value and ends at another.

For counter = startNumber To endNumber (Step increment)
    One or more Visual Basic 2015 statements
Next

If you need to stop the loop before it reaches the end, you can use the Exit For statement inside the loop.

Displaying Numbers 1 to 10

Dim counter As Integer
For counter = 1 To 10
    ListBox1.Items.Add(counter)
Next

This program inserts the numbers 1 through 10 into a ListBox.

Adding a Series of Numbers

The following program calculates the running sum of the numbers:

0 + 10 + 20 + 30 + 40 + ... + 100
Dim counter, sum As Integer
For counter = 1 To 100 Step 10
    sum += counter
    ListBox1.Items.Add(sum)
Next

The Step 10 part increases the counter by 10 each time.

Using a Negative Step

The increment in a For...Next loop can also be negative. The following example computes:

1000 - 100 - 95 - 90 - ... - 5
Dim counter, sum As Integer

sum = 1000
For counter = 100 To 5 Step -5
    sum -= counter
    ListBox1.Items.Add(sum)
Next

Here, the counter starts at 100 and decreases by 5 until it reaches 5.

Exiting a For...Next Loop Early

You can terminate a For...Next loop early by using Exit For. The original lesson’s code appears to contain a small syntax issue, but the intended idea is:

Dim n As Integer
For n = 1 To 10
    If n > 6 Then
        Exit For
    Else
        ListBox1.Items.Add(n)
    End If
Next

This program adds values to the ListBox only while n is 6 or below. As soon as n becomes greater than 6, the loop exits.

The Do Loop

In Visual Basic 2015, there are several forms of the Do Loop. These loops are especially useful when you do not know מראש how many repetitions will be needed.

Common structures include:

Do While condition
    Block of statements
Loop
Do
    Block of statements
Loop While condition
Do Until condition
    Block of statements
Loop
Do
    Block of statements
Loop Until condition

You can also use Exit Do to escape the loop before the condition is reached.

Repeating Until a Number Exceeds 1000

In this example, the procedure keeps increasing the value of counter by 1 until it becomes greater than 1000.

Do While counter <= 1000
    TextBox1.Text = counter
    counter += 1
Loop

The same logic can also be written like this:

Do
    TextBox1.Text = counter
    counter += 1
Loop Until counter > 1000

Both versions achieve the same result, but they test the condition in different positions.

Displaying a Running Sum with Exit Do

The following example repeatedly adds numbers and shows the running total in a ListBox. The process stops after 100 repetitions.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim sum, n As Integer

    ListBox1.Items.Add("n" & vbTab & "Sum")
    ListBox1.Items.Add("----------------")
    Do
        n += 1
        sum += n
        ListBox1.Items.Add(n & vbTab & sum)
        If n = 100 Then
            Exit Do
        End If
    Loop

End Sub

The same loop can also be written more directly as:

Do Until n = 10
    n += 1
    sum += n
    ListBox1.Items.Add(n & vbTab & sum)
Loop

This shows how Do Loop structures can be adjusted depending on the style of logic you prefer.

VB2015 Figure 15.1 Output of looping example

Figure 15.1: Output of the looping example showing n and the running sum

The While...End While Loop

The While...End While loop is very similar to the Do While form. It continues looping as long as the specified condition remains true.

While condition
    Visual Basic 2015 statements
End While

Example 15.3 uses a While loop to generate a running total:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sum, n As Integer
    ListBox1.Items.Add("n" & vbTab & "sum")
    ListBox1.Items.Add("----------------")

    While n <> 10
        n += 1
        sum += n
        ListBox1.Items.Add(n & vbTab & sum)
    End While
End Sub

This loop continues until n becomes 10.

Choosing the Right Loop

Each loop structure is useful in different situations:

  • For...Next is best when the number of repetitions is known in advance.
  • Do...Loop is useful when repetition depends on a condition that may change dynamically.
  • While...End While is useful when you want to repeat actions as long as a condition remains true.

Why Looping Is Important

Core takeaway:

Looping lets your program perform repeated tasks automatically and efficiently. It is one of the most important concepts in programming because it reduces repetition, saves time, and makes your code more powerful.

Build on This Foundation

Continue to VB2026

After learning the basics of looping in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.

Explore VB2026 β†’

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Exercise Questions

  1. What is the main difference between a For...Next loop and a Do Loop?
  2. Write a short VB2015 example that displays the even numbers from 2 to 20 in a ListBox.
  3. Why might you use Exit For or Exit Do in a loop?

Go to Lesson 16

In the next lesson, you will learn how to organize code using Sub Procedures in Visual Basic 2015.