Breaking News
You are here: Home / Notes / SEM-4 / .NET PROGRAMMING / Loops in vb.net

Loops in vb.net

There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: 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.

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:

loop_architecture

 

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 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.
While… End While It executes a series of statements as long as a given condition is True.
With… End With It is not exactly a looping construct. It executes a series of 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.

 

Do Loop

vb.net_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.

The syntax for this loop construct is:

Syntax:

Do { While | Until } condition
    [ statements ]
    [ Continue Do ]
    [ statements ]
    [ Exit Do ]
    [ statements ]
Loop
-or-
Do
    [ statements ]
    [ Continue Do ]
    [ statements ]
    [ Exit Do ]
    [ statements ]
Loop { While | Until } condition

Example:

Module loops
   Sub Main()
      ' local variable definition 
      Dim a As Integer = 10
      'do loop execution 
      Do
          Console.WriteLine("value of a: {0}", a)
          a = a + 1
      Loop While (a < 20)
      Console.ReadLine()
   End Sub
End Module

 

For…Next Loop

vb.net_fornext_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.

The syntax for this loop construct is:

For counter [ As datatype ] = start To end [ Step step ]
    [ statements ]
    [ Continue For ]
    [ statements ]
    [ Exit For ]
    [ statements ]
Next [ counter ]

Example

Module loops
   Sub Main()
      Dim a As Byte
      ' 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 Loop

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.

The syntax for this loop construct is:

For Each element [ As datatype ] In group
    [ statements ]
    [ Continue For ]
    [ statements ]
    [ Exit For ]
    [ statements ]
Next [ element ]

Example

Module loops
   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

vb.net_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
    [ statements ]
    [ Continue While ]
    [ statements ]
    [ Exit While ]
    [ statements ]
End While

Example

Module loops
   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. Following section shows few examples to illustrate the concept.

Syntax

The syntax for a nested For loop statement in VB.Net is as follows:

For counter1 [ As datatype1 ] = start1 To end1 [ Step step1 ]
    For counter2 [ As datatype2 ] = start2 To end2 [ Step step2 ]
    ...
    Next [ counter2 ]
Next [ counter 1]

The syntax for a nested While loop statement in VB.Net is as follows:

While condition1
    While condition2
    ...
    End While
End While

The syntax for a nested Do…While loop statement in VB.Net is as follows:

Do { While | Until } condition1
    Do { While | Until } condition2
    ...
    Loop
Loop

Example

Module loops
   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

 

 

Download Paper Solutions from Below Link

About admin

Scroll To Top