For Loop in

By: Ramlak Emailed: 1757 times Printed: 2444 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]
    [Exit For]
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

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. Home | All Tutorials | Latest 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


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
For Each…Next Loop in
For Loop in
Do Loop in
Setting Up SSL on Tomcat
Unicode and UTF-8 in C
Sample program to demonstrate the use of ActionListener HTTPS hostname wrong: should be PKIX path building failed: unable to find valid certification path to requested target 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
Scope in
Unstructured Exception Handling in
Structured Exception Handling in
Creating Sub Procedures in
Creating Functions in
Passing a Variable Number of Arguments to Procedures in
Specifying Optional Arguments with default values in Procedures in
Preserving a Variable's Values between Procedure Calls in
Procedure Delegates in
Properties in
Understanding Scope in
Using Resume Next and Resume Line in
Using On Error GoTo 0 in
Getting an Exception's Number and Description in
Raising an Exception Intentionally in

More Latest News
Most Viewed Articles (in )
Your first VB.NET Crystal Reports - A step by step guide
How to export from DataGridView to excel using
How to export from database to excel using
Sub Procedures and Functions in
Insert cell data in an Excel file using OLEDB in
A tutorial on Chat Server and Chat Client in
How To Connect To A MySql Database in
Handling Strings in
Using Select Case in
Using Resume Next and Resume Line in
What's New in VB .NET? A comparison of VB vs
Operators in
For Loop in
Handling Dates and Times in
Handling Timer Events - and Creating an Alarm Clock in
Most Emailed Articles (in
ArrayList data structure in
“Using If with And” and Comparing two integers using If
Nested If and Single line if statement
The Select Case statement in
The For Loop in
What's New in VB .NET? A comparison of VB vs
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
Converting between Data Types in
Arrays and Dynamic Arrays in