X



Home>Tutorial>

What is Visual Basic Control Statement and Examples

Nuelson Penuel Thursday, August 3, 2023 Visual Basic.Net

Visual Basic Loop Overview

Visual Basic has the ability to allow you repeat a sequence of codes as many times as long as the processor and memory could support. This is generally called looping . Looping is required when we need to process something repeatedly until a certain condition is reached. For example, we can design a program that adds a series of numbers to a ComboBox until a specific number is reached. In this article am going to explain Visual Basic For Next Loop, Do While Loop and Do Until Loop, While Wend Loop,For Each Next Loop and Examples

Types of Loops

Depending upon the position of a control statement in a program, a loop is classified into two types:
  • Entry controlled loop
  • Exit controlled loop

Entry Controlled Loop

In an entry controlled loop, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop.

Exit Controlled Loop

In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called as a post-checking loop.

Visual Basic For....Next Loop

The For-Next loop can be used to execute a block of code for a specific number of times. The “VBScript For loop" specifies the counter variable and its start and end values. The Next statement increases the counter variable by one.

For Next Syntax


For counter = startNumber To endNumber (Step increment)
	'code to execute
Next
To exit a For.....Next Loop, you can place the Exit For statement within the loop; and it is normally used together with the If...Then...statement.

Visual Basic For-Next Example

A program to add numbers between 1 to 20 to Dropdownlist control

Dim i As Integer
	For  i =  1 To 20
	DropDownList1.Items.Add (i)
Next

Visual Basic For-Step-Next Loop

By default, the loop counter variable is incremented by one. If you want to increase or decrease the counter variable by the value you specified, then you can use For....Step....Next loop. Suppose in the above code, you want to increment the counter by 3, then your code would be like this:

For Next Step Syntax

For i = startNum To endNum Step number
'code goes here
Next

Dim i As Integer
	For  i =  1 To 20 Step 3
	DropDownList1.Items.Add (i)
Next
In the above you have incremented the counter. To decrement, try

Dim i As Integer
	For  i =  1 To 20 Step -3
	DropDownList1.Items.Add (i)
Next
Furthermore, you can also terminate your your For-Next statement at anytime you dim fit. Take for instance the example we used above, if there should be any need for you to stop the loop once the counter reaches 10, then you add "If statement" to exit the loop. Example is

Dim i As Integer
	For  i =  1 To 20 

	DropDownList1.Items.Add (i)
  If i = 10 Then

        Exit For
  End If
Next

Visual Basic For-Each-Next Loop

If you want to repeat a block of code for each item in a collection or for each element of an array, then you need to use For-Each-Next loop.

For Each Next Loop Syntax

For Each item In Items 
'code to execute
Next

For Each Next Loop Example


Dim cars(3)
cars(0)="Toyota"
cars(1)="Camri"
cars(2)="Range Rover"

For Each car In cars
  MsgBox (car)
Next

Visual Basic While Wend Loop

While...Wend loop is similar to Do While loop though not used commonly. As Do While is more structured than While.....Wend loop, is usually mostly used by programmers.

While Wend Syntax


While VariableName
'code to execute
Wend  

Visual Basic While Wend Example


Dim i As Integer
i = 1   
While i
i < 3 
MsgBox ("Welcome")
i = i +1
Wend  

Visual Basic Do Loops

Types of Do Loops in Visual Basic

Do While Loop

At times, you might not know the number of times you need to execute a particular block of code, then you will be using Do While loops. Take for example, you want to display a "Welcome, how are you?" message to any one that launched your application while the counter variable "i" is less than 10. In such case, Do While loop will be perfect choice to use.

Do While Loop Syntax

Do While condition
'code to execute
Loop

Visual Basic Do While Loop Example


Dim i As Integer
i = 2
Do While i < 10
MsgBox("Welcome! How are you?")
i = i + 2
Loop

Visual Basic Do Until Loop

Do Until loop is also used when you do not know the number of times your block of code would be executed.

Visual Basic Do Until Loop Syntax

Do Until condition
'code to execute

Loop
The example given in "Do While" statement can also be written as;

Do Until Loop Example


Dim i As Integer
i = 2
Do Until x=10
MsgBox ("Welcome! How are you?")
i = i + 2
Loop
Or

Dim i As Integer
i = 2
Do 
MsgBox ("Welcome! How are you?")
i = i + 2
Loop Until i = 10

| | Views(557)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services