The For Loop in VB.net

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.


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

PLEASE I WANT A CODE TO BE PUT BEHIND A BUTTON SO THAT IF I CLICK ON IT (1) WILL APPEAR IN THE CORRESPONDING TEXT-BOX AND IF I CLICK ON THE SECOND TIME, (2) SHOULD APPEAR AND SO ON

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 VB.net
Chat Server in VB.net
Chat client in VB.net
How to open and read an XML file in VB.net
How to create an XML file in VB.net
XML and VB.net
How to create an XML file in VB.NET using Dataset
Multi Threaded Client Socket Programming in VB.net
Multi Threaded Server Socket Programming in VB.net
Multi threaded Socket Programming in VB.net
Client Socket Program sample in VB.net

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