Visual Basic loop structures allow you to run one or more
lines of code repetitively. You can repeat the statements in a loop
structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection.
The following illustration shows a loop structure that runs a set of statements until a condition becomes true.
The following illustration shows a loop structure that runs a set of statements until a condition becomes true.
The While...End While construction runs a set of statements as long as the condition specified in the While statement is True.
Runs a series of statements as long as a given condition is True.
While condition [ statements ] [ Continue While ] [ statements ] [ Exit While ] [ statements ] End While
Do Loops
The Do...Loop
construction allows you to test a condition at either the beginning or
the end of a loop structure. You can also specify whether to repeat the
loop while the condition remains True or until it becomes True.
Repeats a block of statements while a Boolean condition is True or until the condition becomes True.
Do { While | Until } condition [ statements ] [ Continue Do ] [ statements ] [ Exit Do ] [ statements ] Loop
For Loops
The For...Next construction performs the loop a set number of times. It uses a loop control variable, also called a counter,
to keep track of the repetitions. You specify the starting and ending
values for this counter, and you can optionally specify the amount by
which it increases from one repetition to the next.
Repeats a group of statements a specified number of times.
For counter [ As datatype ] = start To end [ Step step ] [ statements ] [ Continue For ] [ statements ] [ Exit For ] [ statements ] Next [ counter ]
For Each Loops
Repeats a group of statements for each element in a collection.
For Each element [ As datatype ] In group [ statements ] [ Continue For ] [ statements ] [ Exit For ] [ statements ] Next [ element ]