There may be scenarios when you need to execute a block of code numerous times. In general, statements are executed in the following order: the first statement in a function is executed first, then the second, and so on.
Programming languages have a variety of control structures that enable more complex execution routes.
A loop statement allows us to run a statement or group of statements many times. The general form of a loop statement in most programming languages is as follows:
Following are the types of looping statements available in VB.Net:
Loop Type |
Description |
Do Loop |
It loops through the enclosing block of statements as long as a Boolean condition is True or until the condition becomes True. It might be ended at any time by using the Exit Do statement. |
For...Next |
It repeats a set number of statements, and a loop index counts the number of loop iterations while the loop executes. |
For Each...Next |
It iteratively repeats a set of statements for each element in a collection. This loop is used to access and manipulate all elements in an array or a VB.Net collection. |
While... End While |
As long as a specified condition is True, it will execute a series of statements. |
With... End With |
It's not quite a looping construct. It runs a series of statements that all relate to the same object or structure. |
Nested loops |
You can nest one or more loops within another While, For, or Do loop. |
Loop Control Statements
Control statements in a loop alter the execution sequence. When execution exits a scope, all automated objects produced inside that scope are deleted.
Following control statements are available in VB.Net:
Control Statement |
Description |
Exit statement |
The loop or select case statement is terminated, and execution is transferred to the statement immediately following the loop or select case. |
Continue statement |
This instructs the loop to skip the rest of its body and immediately retest its condition before reiterating. |
GoTo statement |
Control is passed to the labeled statement. However, using the GoTo statement in your program is not recommended. |