Control statements in VB.NET

Control Statements - Loops.

This lesson shows you how to use vb looping statements. In vb, there are four iteration constructs rather than three. There are familiar for, do, while loops and a new one from Visual basic, foreach.

Let us start with while loop.

The Do and While Statements.

List: 1 WhileTest.vb.

'find out the number of digits in a given number.
Imports System
Class WhileTest
Public Shared Sub Main()
Dim i As Integer = 123
Dim count As Integer = 0
Dim n As Integer = i
'while loop may execute zero times.
While i > 0
i = i / 10
End While
Console.WriteLine("Number {0} contains {1} digits.", n, count)
End Sub 'Main
End Class 'WhileTest

The above code shows simple while loop, which finds out the number of digits in a number. for a given number i = 123, the loop will execute tree times and hence the value of count will be three at the end of the while loop.

The above code has one logical flaw. If the number i is set to 0, the output of the code will be "Number 0 contains 0 digits." Actually number 0 is of one digit. Since the condition for the while loop i>0 is false from beginning for the value i=0, the while loop won't execute any time and count will be zero. here is a solution:

List: 2 DoTest.vb.

'find out the number of digits in a given number.

Imports System
Class DoTest
Public Shared Sub Main()
Dim i As Integer = 0
Dim count As Integer = 0
Dim n As Integer = i
Do
i = i / 10
Loop While i > 0
Console.WriteLine("Number {0} contains {1} digits.", n, count)
End Sub 'Main
End Class 'DoTest

The Do-While construct checks condition in the end of the loop. Thus Do-while loop will execute atleast once even though the condition to be checked is false from beginning.

The For Statement.

//For loop with break and continue statements.

List: 3 Fortest.vb.

Imports

System
Class ForTest
Public Shared Sub Main()
Dim i As Integer
While
i < 20
If i = 10 Then
Exit
While
End
If
If
i = 5 Then
GoTo
ContinueWhile1
End If
Console.WriteLine(i)
ContinueWhile1:
End While
End
Sub 'Main
End Class 'ForTest

The For loop is good when we know how many times the loop needs to be executed. the output of the above code will be:

0
1
2
3
4
6
7
8
9

isn't it self explanatory? when i becomes 5, the loop will skip over the remaining statements in the loop and go back to the post loop action. thus 5 is not part of the output. when i becomes 10, control will break out of the loop.

The foreach statement.

This statement allows to iterate over the elements in arrays and collections. here is a simple example.

List: 4 ForEach.vb.

'foreach loo

Imports System
Class ForEach
Public Shared Sub Main()
Dim a As String() = {"Chirag", "Bhargav", "Tejas"}
Dim b As String
For
Each b In a
Console.WriteLine(b)
Next b
End Sub 'Main
End Class 'ForEach

Within the foreach loop parenthesis , the expression is made up of two parts separated by keyword in. The right hand side of in is the collection and left hand side is the variable with type identifier matching to whatever type the collection returns.

Every time the collection is queried for a new value. As long as the collection returns value, the value is put into the variable and expression will return true. when the collection is fully traversed, the expression will return false and control will be transferred to the next statement after a loop.

 

Up Next
    Ebook Download
    View all
    Learn
    View all