The For Loop in VB.net
By: Issac in VB.net Tutorials on 2008-11-25
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- 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
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures 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
Throwing an Exception in VB.net
Comments