Loop Statement in Visual Basic

In this lesson, we’re going to learn how to use loop statements in visual basic.

What is a loop?

A loop in computer programming is a process that repeats the same command or instruction and ends until given condition becomes true or false. Loops are considered one of the most basic concepts and mostly used in computer programming.

In this lesson, we’re going to discuss the different loops in visual basic such as do loop, do while loop, do until loop, for next loop.

Do Loop Statement

There are two types of do loops in visual basic; we have Do While and Do Until.

Do Loop statement is the basic form of loop in visual basic, it has the following structure:

Do
yourStatement
If yourExpression Then Exit Do
Loop

Where:

yourStatement is the block of codes that your program will execute.

yourExpression is a comparison test to be tested if true.

Here’s an example:

Private Sub Command1_Click()
Dim myNumber As Integer
myNumber = 0
Do
myNumber = myNumber + 1
MsgBox myNumber
If myNumber = 5 Then Exit Do
Loop
End Sub

Explanation:

First we have declared a variable myNumber which is an integer type.

  1. We have set the myNumber value to zero.
  2. Then we have initiate a do loop statement.
  3. The loop will evaluate if the value of myNumber is equal to 5, it will prompt a message which displays the value of myNumber and exits the loop if it reaches 5.

Do While Loop Statement

Structure of do while loop:

Do While yourExpression
yourStatement
Loop

The do while loop statement will continue to execute as long as the expression is true, the loop will only end if the expression returns false.

Note: you can declare block of codes as many as you want, but make sure that your expression will have an end (false value) or else your loop will enter an infinite loop.

Example:

Private Sub Command2_Click()
Dim myNumber As Integer
myNumber = 0
Do While myNumber < 5
myNumber = myNumber + 1
MsgBox myNumber
Loop
End Sub

Explanation:

  1. We have declared a myNumber variable as integer type and set its value to zero.
  2. Now we will use the do while statement to test if the value of our variable (myNumber) is less than 5.
  3. If it’s true, the number will now increment by one until it reaches 5.
  4. The loop will end if the value of our variable is 5 (expression becomes false)

Do Until Loop

It is a type of loop that evaluates or test the expression if false, which means the block of codes will execute until the expression becomes false.

Here’s the structure of do until loop:

Do Until yourExpression
yourStatement
Loop

Where:

yourStatement is the block of codes that your program will execute.

yourExpression is a comparison test to be tested if false.

Example:

Private Sub Command3_Click()
Dim myNumber As Integer
myNumber = 0
Do Until myNumber > 5
myNumber = myNumber + 1
MsgBox myNumber
Loop
End Sub

Explanation:

  1. We have declared a myNumber variable as integer type and set its value to zero.
  2. Now we will use the do until statement to test if the value of our variable (myNumber) is greater than 5.
  3. The number will now increment by one until the number is greater than 5.
  4. If the value of our variable is greater than 5 then the expression becomes false and ends the loop.

For Next Loop Statement

For loop is another method of creating loop in visual basic.

Structure:

For counterVariable = startValue To endValue
yourStatement
Next counterVariable

Where:

counterVariable is simple the name of your variable.

startValue is a value that you want to start the loop. It can be a number, variable or an expression.

endValue is the value that you want to end the loop. It can be a number, variable or an expression.

yourStatement is the line of codes you want to execute.

For loop will execute the body of your program for a specified number of times. In this loop, you can specify what value to start and what value to end.

Example:

Private Sub Command4_Click()
Dim myNumber As Integer
myNumber = 0
For myNumber = 1 To 5
MsgBox myNumber
Next myNumber
End Sub

Explanation:

  1. We have declared a myNumber variable as integer type and set its value to zero.
  2. Now we will use for next loop. We have set 1 as the starting value and end with number 5.
  3. The message box will continue to display until it reaches to number 5.
  4. That ends the for next loop.
, , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.