3. 3
What is Looping Statement?
A Loop is used to repeat the same process multiple times until it meets the
specified condition in a program.
By using a loop in a program, a programmer can repeat any number of statements
up to the desired number of repetitions.
A loop also provides the suitability to a programmer to repeat the statement in a
program according to the requirement.
A loop is also used to reduce the program complexity, easy to understand, and
easy to debug.
4. 4
Advantages of Looping Statements
It provides code iteration functionality in a program.
It executes the statement until the specified condition is true.
It helps in reducing the size of the code.
It reduces compile time.
5. 5
Types of Loop Constructs
VB.NET supports following types of loop:
1. While..End While
2. Do…..Loop
3. For … Next
4. For Each… Next
6. 6
While… End Loop Constructs
The While End loop is used to execute
blocks of code or statements in a program,
as long as the given condition is true.
It is useful when the number of executions
of a block is not known. It is also known as
an entry-controlled loop statement, which
means it initially checks all loop conditions.
If the condition is true, the body of the while
loop is executed. This process of repeated
execution of the body continues until the
condition is not false. And if the condition is
false, control is transferred out of the loop.
7. 7
While Loop-Syntax & Example
Example:
Imports System
Module while_number
Sub Main()
'declare x as an integer variable
Dim x As Integer
x = 1
' Use While End condition
While x <= 10
'If the condition is true, the statement will be executed.
Console.WriteLine(" Number {0}", x)
x = x + 1 ' Statement that change the value of the
condition
End While
Console.WriteLine(" Press any key to exit...")
Console.ReadKey()
End Sub
End Module
Syntax:
While [condition]
[ Statement to be executed ]
End While
8. 8
Do-While Loop
In VB.NET, Do While loop is used to
execute blocks of statements in the
program, as long as the condition
remains true.
It is similar to the While End Loop,
but there is slight difference between
them.
The while loop initially checks the
defined condition, if the condition
becomes true, the while loop's
statement is executed. Whereas in
the Do loop, is opposite of the while
loop, it means that it executes the Do
statements, and then it checks the
condition.
9. 9
Do-While Loop
Example:
Imports System
Module Do_loop
Sub Main()
' Initializatio and Declaration of variable i
Dim i As Integer = 1
Do
' Executes the following Statement
Console.WriteLine(" Value of variable I is : {0}", i)
i = i + 1 'Increment the variable i by 1
Loop While i <= 10 ' Define the While Condition
Console.WriteLine(" Press any key to exit...")
Console.ReadKey()
End Sub
End Module
Syntax:
Do
[ Statements to be executed]
Loop While Boolean_expression
// or
Do
[Statement to be executed]
Loop Until Boolean_expression
10. 10
For…Next Loop
A For Next loop is used to
repeatedly execute a sequence of
code or a block of code until a given
condition is satisfied.
A For loop is useful in such a case
when we know how many times a
block of code has to be executed. In
VB.NET, the For loop is also known
as For Next Loop.
11. 11
For Loop-Syntax
For: It is the keyword that is present at the beginning of the definition.
variable_name: It is a variable name, which is required in the For loop Statement. The value of the variable
determines when to exit from the For-Next loop, and the value should only be a numeric.
[Data Type]: It represents the Data Type of the variable_name.
start To end: The start and end are the two important parameters representing the initial and final values of
the variable_name. These parameters are helpful while the execution begins, the initial value of the variable is set by
the start. Before the completion of each repetition, the variable's current value is compared with the end value. And if
the value of the variable is less than the end value, the execution continues until the variable's current value is greater
than the end value. And if the value is exceeded, the loop is terminated.
Step: A step parameter is used to determine by which the counter value of a variable is increased or decreased after
each iteration in a program. If the counter value is not specified; It uses 1 as the default value.
Statements: A statement can be a single statement or group of statements that execute during the completion of
each iteration in a loop.
Next: In VB.NET a Next is a keyword that represents the end of the For loop's
Syntax:
For variable_name As [ DataType ] = start To end [ Step step ]
[ Statements to be executed ]
Next
12. 12
For Loop- Example
Imports System
Module Number
Sub Main()
' It is a simple print statement, and 'vbCrLf' is used to jump in the next line.
Console.Write(" The number starts from 1 to 10 " & vbCrLf)
' declare and initialize variable i
For i As Integer = 1 To 10 Step 1
' if the condition is true, the following statement will be executed
Console.WriteLine(" Number is {0} ", i)
' after completion of each iteration, next will update the variable counter
Next
Console.WriteLine(" Press any key to exit... ")
Console.ReadKey()
End Sub
End Module
13. 13
For…Each…Next Loop
In the VB.NET, For Each loop is used to
iterate block of statements in an array or
collection objects.
Using For Each loop, we can easily work
with collection objects such as lists, arrays,
etc., to execute each element of an array or
in a collection. And when iteration through
each element in the array or collection is
complete, the control transferred to the next
statement to end the loop.
For Each loop is used to read each
element from the collection object or an
array.
The Data Type represents the type of the
variable, and var_name is the name of the
variable to access elements from
the array or collection object so that it can
be used in the body of For Each loop.
14. 14
For Each Loop-Syntax & Example
Syntax:
For Each var_name As [ DataType ] In Collection_Object
[ Statements to be executed]
Next
Imports System
Module For_Each_loop
Sub Main()
'declare and initialize an array as integer
Dim An_array() As Integer = {1, 2, 3, 4, 5}
Dim i As Integer 'Declare i as Integer
For Each i In An_array
Console.WriteLine(" Value of i is {0}", i)
Next
Console.WriteLine("Press any key to exit...")
Console.ReadLine()
End Sub
End Module