While Loop in VB.net
By: Ramlak
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. |
Archived Comments
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net
Exception Filtering in the Catch Block in VB.net
Using Multiple Catch Statements in VB.net
Throwing an Exception in VB.net
Throwing a Custom Exception in VB.net
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net