Programming Tutorials

While Loop in VB.net

By: Ramlak in VB.net Tutorials on 2008-11-25  

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.
Press any key to continue
Tip 

Many Visual Basic functions, like EOF-which is true when you've reached the end of a file while reading from it-are explicitly constructed to return values of True or False so that you can use them to control loops such as Do and While loops.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)