Hi friend
I want to know what is loop in VB.NET with example.
thank you.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SenthilkumarPosted May 7, 2012, 10:54 PM
There are nice articles which explains the looping with visual output.
http://www.startvbdotnet.com/language/loops.aspx
http://www.dotnetperls.com/for-vbnet
http://www.homeandlearn.co.uk/net/nets3p2.html
Satyapriya NayakPosted May 7, 2012, 9:58 PM
While Loop in VB.net
While loops keep looping while the condition they test remains true, so you use a While loop if you have a condition that will become false when you want to stop looping. Here's the While loop's syntax (note that you used to end this loop with Wend in VB6 and before—that's changed to End While now):
While condition
[statements]
End While
And here's an example putting While to work:
Sub CheckWhile()
Dim intCounter As Integer = 0
Dim intNumber As Integer = 10
While intNumber > 6
intNumber -= 1
intCounter += 1
End While
MsgBox("The loop ran " & intCounter & " times.")
End Sub
And here's what you see when you run this code:
The loop ran 4 times.
Do Loop in VB.net
The Do loop keeps executing its enclosed statements while or until (depending on which keyword you use, While or Until) condition is true. You can also terminate a Do loop at any time with an Exit Do statement. The Do loop has two versions; you can either evaluate a condition at the beginning:
Do [{While | Until} condition ]
[statements]
[Exit Do]
[statements]
Loop
or at the end:
Do
[statements]
[Exit Do]
[statements]
Loop [{While | Until} condition]
Here's an example where the code keeps displaying the message "What should I do?" until the user types "Stop" (note that I'm using UCase to uppercase what the user types and comparing it to "STOP" to let them use any combination of case when they type "Stop"):
Module Module1
Sub Main()
Dim strInput As String
Do Until UCase(strInput) = "STOP"
System.Console.WriteLine("What should I do?")
strInput = System.Console.ReadLine()
Loop
End Sub
End Module
For Loop in VB.net
The For loop is probably the most popular of all Visual Basic loops. The Do loop doesn't need aloop index, but the For loop does; a loop index counts the number of loop iterations as the loop executes. Here's the syntax for the For loop—note that you can terminate a For loop at any time with Exit For:
For index = start To end [Step step]
[statements]
[Exit For]
[statements]
Next [index]
The index variable is originally set to start automatically when the loop begins. Each time through the loop, index is incremented by step (step is set to a default of 1 if you don't specify a value) and when index equals end, the loop ends.
Here's how to put this loop to work; in this case, I'm displaying "Hello from Visual Basic" four times (that is, intLoopIndex will hold 0 the first time; 1, the next; followed by 2; and then 3, at which point the loop terminates):
Module Module1
Sub Main()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To 3
System.Console.WriteLine("Hello from Visual Basic")
Next intLoopIndex
End Sub
End Module
Here's what you see when you run this code:
Hello from Visual Basic
Hello from Visual Basic
Hello from Visual Basic
Hello from Visual Basic
Press any key to continue
If I were to use a step size of 2:
For intLoopIndex = 0 To 3 Step 2
System.Console.WriteLine("Hello from Visual Basic")
Next intLoopIndex
I'd see this result:
Hello from Visual Basic
Hello from Visual Basic
Also Please refer the below link
http://www.startvbdotnet.com/language/loops.aspx
http://www.vbtutor.net/lesson9.html
Thanks