ARTICLE

Control statements in VB.NET

Posted by Amisha Mehta Articles | Visual Basic 2010 June 13, 2003
In this article, you will learn various control statements (looping) controls in VB.NET including Do..While, For, and foreach statements.
 
Reader Level:

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 loop
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.

share this article :
post comment
 

How to increment the value in textbox while loading the dform in vb.net windows applicaion

Posted by srinivasan s Sep 29, 2010

this article is very simple and easy to understand

Posted by Anne Naveen Nov 25, 2008

sir actually these examples are not enough to get the more knowledge in vb .net

Posted by suresh shankar Oct 08, 2008
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor