For Loop in VB.net

By: Ramlak Emailed: 1700 times Printed: 2205 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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

Although it's been common practice to use a loop index after a loop completes (to see how many loop iterations were executed), that practice is now discouraged by people who make it their business to write about good and bad programming practices.


VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

wehhh... so so so difficult

View Tutorial          By: sam at 2013-02-28 01:55:41

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ramlak
While Loop in VB.net
For Each…Next Loop in VB.net
For Loop in VB.net
Do Loop in VB.net
Setting Up SSL on Tomcat
Unicode and UTF-8 in C
Sample program to demonstrate the use of ActionListener
java.io.IOException: HTTPS hostname wrong: should be
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
NotifyUtil::java.net.ConnectException: Connection refused: connect
'LINK.EXE' is not recognized as an internal or ext
Using Transactions in JDBC
What is the ACID principal?
How connection pooling works in Java and JDBC
A simple JDBC application sample code

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
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

More Latest News
Most Viewed Articles (in VB.net )
For Loop in VB.net
“Using If with And” and Comparing two integers using If
Nested If and Single line if statement
The Select Case statement in VB.net
The For Loop in VB.net
What's New in VB .NET? A comparison of VB vs VB.net
What is .NET Framework and the Common Language Runtime?
Debug and Release Versions in .NET
File Extensions Used in VB .NET
Assemblies, Solutions and Projects in VB .NET
The Option and Imports Statements in VB .NET
Visual Basic Statements
Data types in VB.net
Converting between Data Types in VB.net
Arrays and Dynamic Arrays in VB.net
Most Emailed Articles (in VB.net)
Chat Server in VB.net
Procedure Delegates in VB.net
File operations in VB.net
Excel 2007 Data Validation Input Message using VB.net
Multi threaded Socket Programming in VB.net
XML and VB.net
How to create an XML file in VB.net
A tutorial on Chat Server and Chat Client in VB.net
Creating a Web Service in VB.net
“Using If with And” and Comparing two integers using If
Nested If and Single line if statement
The Select Case statement in VB.net
The For Loop in VB.net
What's New in VB .NET? A comparison of VB vs VB.net
What is .NET Framework and the Common Language Runtime?