October 2016
October 2016
Sub Main()
End Module
For …Next Loop
• 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…Next Syntax
For counter [ As datatype ] = start To end [ Step
step ]
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]
Flow Diagram
Example- For Next
Module Module1
Sub Main()
Dim a As Integer
' for loop execution
For a = 10 To 20
Console.WriteLine("value of a: {0}", a)
Next
Console.ReadLine()
End Sub
End Module
For each…next
• It repeats a group of statements for each
element in a collection. This loop is used for
accessing and manipulating all elements in an
array or a VB.Net collection.
Syntax
For Each element [ As datatype ] In group
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]
Example
Module Module1
Sub Main()
Dim anArray() As Integer = {1, 3, 5, 7, 9}
Dim arrayItem As Integer
'displaying the values
For Each arrayItem In anArray
Console.WriteLine(arrayItem)
Next
Console.ReadLine()
End Sub
End Module
While ….End While 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.
Syntax
While condition
[ statements ]
[ Continue While ]
[ statements ]
[ Exit While ]
[ statements ]
End While
• statement(s) may be a single statement or a block
of statements.
• The condition may be any expression, and true is
logical true.
• The loop iterates while the condition is true.
• When the condition becomes false, program
control passes to the line immediately following
the loop
• NB: While loop 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
Example – While Loop
Module Module1
Sub Main()
Dim a As Integer = 10
' while loop execution '
While a < 20
Console.WriteLine("value of a: {0}", a)
a = a + 1
End While
Console.ReadLine()
End Sub
End Module
Nested Loops
• VB.Net allows using one loop inside another
loop (Nesting)
• Following section shows few examples to
illustrate the concept:
Nested For Loop- Syntax
For counter1 [ As datatype1 ] = start1 To end1 [
Step step1 ]
For counter2 [ As datatype2 ] = start2 To end2 [ Step step2
]
...
Next [ counter2 ]
Next [ counter 1]
Nested While Loop- Syntax
While condition1
While condition2
...
End While
End While
Nested Do While - Example
Do { While | Until } condition1
Do { While | Until } condition2
...
Loop
Loop
Sub Main()
' local variable definition
Dim i, j As Integer
For i = 2 To 100
For j = 2 To i
' if factor found, not prime
If ((i Mod j) = 0) Then
Exit For
End If
Next j
If (j > (i \ j)) Then
Console.WriteLine("{0} is prime", i)
End If
Next i
Console.ReadLine()
End Sub
End Module
With…End With
• It is not a looping construct actually.
• It executes a series of statements that
repeatedly refers to a single object or
structure
The syntax is:
With object
[ statements ]
End With
Example - With…End With
Module Module1
'class definition
Public Class Book
Public Property Name As String
Public Property Author As String
Public Property Subject As String
End Class
Sub Main()
Dim aBook As New Book 'object declaration
'with block
With aBook
.Name = "VB.Net Programming"
.Author = "Nyabiko"
.Subject = "Information Technology"
End With
With aBook
Console.WriteLine("Book Title :" + .Name)
Console.WriteLine("Author Name :" + .Author)
Console.WriteLine("Subject Area :" + .Subject)
End With
Console.ReadLine()
End Sub
Additional Resources:
• https://www.dotnetperls.com/for-vbnet
• https://www.tutorialspoint.com/vb.net/vb.net
_loops.htm