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

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

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

1-While:
Syntax: while <condition>
           <stmts>
         [exit while]
         end while
2-do While:
Syntax: Do while <condition>
             <stmts>
            [exit do]
           loop
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>
            <stmts>
          exit do
         loop

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]
            <statements>
            [exit for]
          Next [<var>]

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

Control chars.tab 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 & Controlchars.tab)
Next

5-Foreach:
Syntax: Foreach <var> in <collection>
         <stmts>
        [exit for]
        next

A 10 20 30 40 50
Foreach N in A
console.writeline(N)
next

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.

Vb.net 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
           <statements>
           [exit do]
           loop while<condition>

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

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




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