Follow in Twitter & Facebook

Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools

Tuesday, May 8, 2012

Looping control statements in VB.Net

Here we will discuss about different looping control statements in VB.Net. Also you can check my articles on SQL Server and Asp.Net.

There are different looping control statements available in VB.Net. These are

Syntax: while <condition>
         [exit while]
         end while
2-do While:
Syntax: Do while <condition>
            [exit do]
There is no difference between while loop and do while loop in VB.Net.
Do While provides as a contrast to "doUntil".

3-Do Until:
Syntax: Do Until <condition>
          exit do

Do until loop will execute while condition is false. But while or do while loop executed while
the condition is true.
In case of Do Until statement in loop will be executed when condition is false and exists from the
loop when condition is true.

4-For :
Syntax: For<ar> = <initial value> to <end value> [step n]
            [exit for]
          Next [<var>]

DIM N as integer
for N=1 to 10 step 1
console.write(N & control
Next N

Control is same as "\n" in c.
By default for loop increment the value of variable is 1.

Dim N as integer
For N = 10 to 1 step-1
console.write(N &

Syntax: Foreach <var> in <collection>
        [exit for]

A 10 20 30 40 50
Foreach N in A

Foreach is only possible in case of one dimensional array but not possible incase of two dimensional array.

All the above looping statements are entry controled loops . Which will check the condition while entering into the loop. Hence Statements in the loop will not be executed even when the condition is false. provide the following Exit controled loops that will check the condition while exit the loop. As they check the condition while exit the loop, statements in the loop will be executed atleast once even when the condition is false..

i-do..Loop while:
Syntax:   do
           [exit do]
           loop while<condition>

ii-do...Loop Until:
Syntax:   do
           [exit do]
           Loop until<condition>

So thease are the different loop control statements in VB.Net.

0 on: "Looping control statements in VB.Net"