The For Loop in

By: Issac Emailed: 1758 times Printed: 2452 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

We'll use one to add up our 4 numbers, and then discuss the code. Study the following. In fact, create a new Project. Add a button to your new Form. Double click your new button and type the following code for it:

Dim ans As Integer

Dim start As Integer

ans = 0

For start = 1 To 4

ans = ans + start

Next start

MsgBox ans

Run the program and see what happens when you click the button. The number 10 should have been displayed in your message box.

The For loop code

We start by setting up two integer variables. We set one of these to zero. Then we start our loop code. Let's examine that in more detail.

For start = 1 To 4

ans = ans + start

Next start

We start our loop by telling Visual Basic what type of loop we want to use. In this case it is a For loop:

For start = 1 To 4

The next thing you have to do is tell Visual Basic what number you want the loop to start at:

For start = 1 To 4

Here we are saying "Start the loop at the number 1". The variable start can be called anything you like. A popular name to call a start loop variable is the letter i ( i = 1). So what we're doing is setting up a variable - the start of the loop variable - and putting 1 into it;

Next, you have to Tell Visual Basic what number to end the loop on:

For start = 1 To 4

The To word, followed by a number or variable, tells Visual Basic how many times you want the loop to go round and round. We're telling Visual Basic to loop until the “start” variable equals 4

The command that tells Visual basic to grab the next number in the sequence is this:

Next start

When Visual Basic reaches this line, it checks to see what is in the variable “start”. It then adds one to it. In other words, "Get me the next number after the one I've just used."

The next thing that happens is that Visual Basic will return to the word For. It returns because it’s in a loop. It needs to know if it can stop looping. To check to see if it can stop looping, it skips the start = 1 part, and then jumps to your end number. In our case, the end number was 4. Because Next start adds one to whatever is in start, then start is now 2 (It was 1 at the start. The next number after one is 2 isn’t it?)

So if start is now 2, can Visual Basic stop looping? No it can’t. Because we’ve told it to loop until it reaches number 4. It’s only reached number 2, so off it goes on another trip around the loop. When the start is greater than the end number, Visual Basic drops out of the loop and continues on its way.

But remember why we're looping: so that we can execute some code over and over again.

To clarify things, change the above code to this:

Dim start As Integer

For start = 1 To 4

MsgBox("Start = " & start)

Next start

Run the programme, and click your button. What happens? You should have seen this in the message box, one after the other:

Start = 1

Start = 2

Start = 3

Start = 4

Each time round the loop, the code for the message box was executed. You had to click OK four times - start = 1 To 4. 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


View Tutorial          By: KALUSHA at 2012-07-02 12:32:56

Your name (required):

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

Your sites URL (optional):

Your comments:

More Tutorials by Issac
Save/Write/Read image file from/to a database using Java program
How to modify the objects using java classes
Java program for Cloning
Java program for changeable wrapper class
A tutorial on Chat Server and Chat Client in
Chat Server in
Chat client in
How to open and read an XML file in
How to create an XML file in
XML and
How to create an XML file in VB.NET using Dataset
Multi Threaded Client Socket Programming in
Multi Threaded Server Socket Programming in
Multi threaded Socket Programming in
Client Socket Program sample in

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 )
The For Loop in
“Using If with And” and Comparing two integers using If
Nested If and Single line if statement
The Select Case statement 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
Handling Strings in
Most Emailed Articles (in
If…Else Statements in
Insert Background Pictures in Excel 2007 from VB.NET
How to export from DataGridView to excel using
Specifying Optional Arguments with default values in Procedures in
Arrays and Dynamic Arrays in
ArrayList data structure in
Substring in Vb.Net String Class binary reader and writer
To read the entire worksheet in an Excel workbook through Code
Using Excel 2007 DataBar in
Excel 2007 Data Validation Input Message using
Read Data from Excel using OLEDB in VB.NET 2005
Insert cell data in an Excel file using OLEDB in
How to export from database to excel using
How to send email using VB.NET code