Académique Documents
Professionnel Documents
Culture Documents
The first statement in a function is executed first, followed by the second, and
so on.
Programming languages provide various control structures that allow for more
complicated execution paths.
Loops
A loop statement allows us to execute a
statement or group of statements multiple times
and following is the general form of a loop
statement in most of the programming
languages:
Loops
A loop statement allows us to execute a
statement or group of statements multiple times
and following is the general form of a loop
statement in most of the programming
languages:
VB.Net provides following types of loops
Loop Type Description
Do Loop It repeats the enclosed block of statements while a Boolean condition
is True or until the condition becomes True. It could be terminated at
any time with the Exit Do statement.
For...Next It repeats a group of statements a specified number of times and a
loop index counts the number of loop iterations as the loop executes.
For It repeats a group of statements for each element in a collection. This
Each...Next loop is used for accessing and manipulating all elements in an array or
a VB.Net collection.
VB.Net provides following types of loops
Loop Type Description
While... End It executes a series of statements as long as a given condition is
While True.
With... End It is not exactly a looping construct. It executes a series of
With statements that repeatedly refer to a single object or structure.
Nested loops You can use one or more loops inside any another While, For or
Do loop.
Loop Control Statements:
Loop control statements change execution from its normal sequence. When execution
leaves a scope, all automatic objects that were created in that scope are destroyed.
Control Statement Description
Exit statement Terminates the loop or select case statement and transfers execution
to the statement immediately following the loop or select case.
Continue Causes the loop to skip the remainder of its body and immediately
statement
retest its condition prior to reiterating.
GoTo statement Transfers control to the labeled statement. Though it is not advised to
use GoTo statement in your program.
Loop Control Statements:
Loop control statements change execution from its normal sequence. When execution
leaves a scope, all automatic objects that were created in that scope are destroyed.
Control Statement Description
Exit statement Terminates the loop or select case statement and transfers execution
to the statement immediately following the loop or select case.
Continue Causes the loop to skip the remainder of its body and immediately
statement
retest its condition prior to reiterating.
GoTo statement Transfers control to the labeled statement. Though it is not advised to
use GoTo statement in your program.
Do Loop
It repeats the enclosed block of statements
while a Boolean condition is True or until the
condition becomes True.
A Do...Loop Until loop is a loop that runs until the loop's condition is true, the
condition being checked after each iteration of the loop.
A Do...Loop While loop runs until the loop's condition becomes false. Its
condition is checked after each iteration of the loop.
A For loop iterates a certain number of times, the value of the counter
variable changing each iteration.
Example C
For counter=1000 to 5 step -5
counter=counter-10
Next
Examples:
Module loops
value of a: 10
Sub Main()
value of a: 11
Dim a As Byte
value of a: 12
' for loop execution
value of a: 13
For a = 10 To 20
value of a: 14
Console.WriteLine("value of a: {0}", a)
value of a: 15
Next
value of a: 16
Console.ReadLine()
value of a: 17
End Sub
value of a: 18
End Module
value of a: 19
Examples:
Module loops
value of a: 10
Sub Main()
value of a: 12
Dim a As Byte
value of a: 14
' for loop execution
value of a: 16
For a = 10 To 20 Step 2
value of a: 18
Console.WriteLine("value of a: {0}", a)
value of a: 20
Next
Console.ReadLine()
End Sub
End Module
Examples: Exit a Loop
For n=1 to 10
If n>6 then
Exit For
End If
Else
Print n
End If
Nested For...Next Loop
When you have a loop within a loop, then you have created a nested loop.
You can actually have as many loops as you want in a nested loop provided the
loops are not the never-ending type.
For a nested loop that consists of two loops, the first cycle of the outer loop will be
processed first, then it will process the whole repetitive process of the inner loop,
then the second cycle of the outer loop will be processed and again the whole
repetitive process of the inner loop will be processed.
The program will end when the whole cycle of the outer loop is processed.
The Structure of a nested loop
For counter1=startNumber to endNumber
(Step increment)
For counter2=startNumber to endNumber
(Step increment)
One or more VB statements
Next counter2
Next counter1
The Structure of a nested loop
Private Sub Form_Activate ( )
For firstCounter= 1to 5
Print Hello
For secondCounter=1 to 4
Print Welcome to the VB tutorial
Next secondCounter
Next firstCounter
Print Thank you
End Sub
For Each...Next Loop
It repeats a group of statements The syntax for this loop construct is:
for each element in a collection.
This loop is used for accessing For Each element [ As datatype ] In group
and manipulating all elements in [ statements ]
an array or a VB.Net collection. [ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]
For Each...Next Loop
Example:
Output
Module loops
Sub Main() 1
Dim anArray() As Integer = {1, 3, 5, 7, 9} 3
Dim arrayItem As Integer
'displaying the values 5
For Each arrayItem In anArray
Console.WriteLine(arrayItem) 7
Next 9
Console.ReadLine()
End Sub
End Module
While... End While Loop
It executes a series of statements as long as a given condition is True.
The syntax for this loop construct is:
While condition Statement(s) may be a single statement or a block of
[ statements ] statements.
[ Continue While ] The condition may be any expression, and true is
[ statements ] logical true.
[ Exit While ] The loop iterates while the condition is true.
[ statements ] When the condition becomes false, program control
End While passes to the line immediately following the loop.
While... End While Loop
Here, key point of the While loop is that the
loop might not ever run.
When the condition is tested and the result is
false, the loop body will be skipped and the
first statement after the while loop will be
executed.
While... End While Loop
The While...End While loop executes a block of statement as long as
condition is true.
The While loop has the following syntax:
While condition
statement-block
' While...End While loop in VB.NET
End While
While i < 10
Console.WriteLine(i)
i=i+1
End While
Example:
Module loops
value of a: 10
Sub Main()
Dim a As Integer = 10 value of a: 11
' while loop execution ' value of a: 12
While a < 20 value of a: 13
Console.WriteLine("value of a: {0}", a) value of a: 14
a=a+1 value of a: 15
End While value of a: 16
Console.ReadLine() value of a: 17
End Sub value of a: 18
End Module value of a: 19
With... End With Statement
It is not exactly a looping construct. It executes a series of statements that
repeatedly refers to a single object or structure.