VB.Net - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

VB.Net Loops

VB.Net Loops

shape Description

A loop is used to execute a set of statements repeatedly. The loop is used to perform execution in a sequential manner. Three different types of loops in VB.Net. All the loops are similar, but there is a little difference in the way of execution.

For...Next Loop

shape Description

For...Next Loop is used to iterate a set of statements based on the certain condition once per the iteration. We have to declare a conditional variable or counter or Loop Control and also starting and ending values for the conditional variable. We can declare an increment value optionally.The following is the syntax for the For...Next Loop. [vbnet] For counter [As datatype] = starting_value To ending_value [Step step] [Statements] Next [counter] [/vbnet]

shape Example

[vbnet] Module Module1 Sub Main() For i As Integer = 10 To 20 Console.WriteLine(i) Next Console.ReadKey() End Sub End Module [/vbnet] Output:

For Each Loop

shape Description

For Each Loop is used to iterate a set of statements for the group. We have to declare a conditional variable or counter or Loop Control but, no need to give starting and ending values for the conditional variance. Simple, For Each Loop is used to perform certain modifications for the elements in the group. The following is the syntax of the For Each Loop. [vbnet] For Each Element[As datatype] In Group [Statements] Next [Element] [/vbnet]

shape Example

[vbnet] Module Module1 Sub Main() Dim LIST As New List(Of String) From {"Welcome", "to", "SPlessons"} For Each element As String In LIST Console.WriteLine(element"") Next Console.ReadKey() End Sub End Module [/vbnet] Output:

While Loop

shape Description

While Loop is used to iterate a set of statements based on the condition specified in the While. The iteration will continue until the condition is TRUE. The following is the syntax of the While Loop. [vbnet] While Condition [statements] End While [/vbnet]

shape Example

[vbnet] Module Module1 Sub Main() Dim i As Integer = 0 While i <= 10 Console.WriteLine(i.ToString()) i = i + 1 End While Console.ReadKey() End Sub End Module [/vbnet] Output:

Do While Loop

shape Description

Do While Loop is used to iterate a set of statements based on the condition which is mentioned either, in the start of the loop or in the end of the loop. The iteration will continue until the condition is TRUE. The following is the syntax for the Do While Loop. [vbnet] Do { While or Until } condition [ statements ] Loop [/vbnet] [vbnet] Do [ statements ] Loop { While or Until } condition [/vbnet]

shape Example

[vbnet] Module Module1 Sub Main() Dim i As Integer = 0 Do Console.WriteLine(i) i = i + 1 Loop Until i < 20 Console.ReadKey() End Sub End Module [/vbnet] Output:

Summary

shape Points

  • Vb.Net also contains some control statements same as in other Object Oriented Languages such as continue, exit, GoTo statements.
  • Loops are used to reuse logic multiple times.