«

»

May 03

Understanding “For Loops” in VB.NET

“For Loops” are the most common type of loop that most application developers use when coding. The good thing about “For loops” is that you get to tell the program how many times you want it to loop in advance. I personally think “For Loops” are the easiest of the three loops to become familiar with.. You can use a “For Loop” if you declare at least two variables with an integer datatype.

Syntax of “For Loops”

For InsertDeclaredVariableHere = InsertStartNumberHere To InsertEndNumberHere

VariableName=VarialbeName+InsertDeclaredVariableHere

Next

Above is just a standard syntax to help you. Make adjustments if you are doing multiplication, division, etc.

Example of For Loop In Action

Say you want your application to loop until as many times as the number you enter into a textbox and give the total of the sum of the number entered. Well as I mentioned earlier, a “For Loop” would be ideal because you already know how many times you want it to loop. You want it to loop as many times as the number you entered in the textbox. So if you input 3 in a textbox, you know that you want it to loop 3 times. Remember again that you need at least two variables declared as an integer data type to use a For loop. “Counter should at least be set to 1 in your for loop statement.

Option Strict On
Imports System.Convert

The code below here should be entered into an event handler that handles Button Click event. (Just create a button and double click it to create the procedure handler)

Dim Number, counter, Answer as Integer.
Number=ToInt32(txtNumber.text)
For counter = 1 to Number
Answer= Answer+Number
Next
label1.text=answer.Tostring

If you run this code and enter 5 in the textbox, the label will return 25. If you enter 4, it will return 16. If you enter 3 it will return 9 and so forth. . If you entered five, it will loop five times and keep adding five to the variable named “answer” every time it loops. 5+5+5+5+5 = 25. If you entered 4, it will loop four times. 4+4+4+4=16. if you entered 3, it will loop 3 times. 3+3+3=9…. if you entered 2, it will loop two times. 2+2=4. if you enter 1, it will loop one time. 1+1 = 2. Basically if you enter any number in the textbox it will loop that many times.. Try entering 50 and watch what number you will get. You will get 2500 because it looped 50 times and each time it looped it added 50.. The reason you are the getting sum of the loops is because in your loop statement you said Answer= Answer+Number. The variable “Number” becomes the value you enter in the textbox. That is a basic addition problem.

The Variable “Answer” = 0 by default since you didnt say it equals anything but as it loops, “answer” first becomes the number that you entered in the textbox, then when it loops again, it adds the same number and then that gets stored and so forth. This is why its called a loop. If you entered, 2 in the textbox, 2 would get stored in the variable “answer” but then as it loops it ADDs 2 again, so 2+2=4..

Tip: Answer=Answer+Number can also be written as Answer+=Number. If you are not familiar with loops just yet, I would suggest just writing it as Answer=Answer+Number so you can get a better visual.

  • Thomas Jefferson

    TURTLES