For loops

Hb_Kai

Member
Joined
Dec 1, 2008
Messages
10
Programming Experience
Beginner
Hi,
I just started learning VB.NET last night as I was told it's one of the best programming languages to begin learning for someone wanting to be a programmer.
Anyway, I've been following up this tutorial which I've been doing quite well until today when I got up to For loops and I'm finding it hard to understand because it feels like my brain is going here, there, and everywhere.

I'm not sure if it's me or if it's the tutorial that's making it confusing but I was wondering if somebody here could give me a hand?

Ta for the help.
 
Programming does not exist in a vacuum. Programming concepts are pretty much all analogous to real world concepts. Consider an egg carton full of eggs. Assume that the cups are numbered from 0 to 11. Now, if I said to you, for a number i where i goes from 0 to 11, take egg number i and crack it into a bowl, you could do that, right? Congratulations! You just implemented a For loop.

A For loop has a loop control variable, also called a loop counter, that starts at a specific value and increments each iteration until it reaches a specific end value. Once the next iteration completes the loop ends. Within the loop you can use that loop control variable for whatever purpose is appropriate. The most common use is as an index into an array or collection, a la my egg carton example. That's not the only use though. Lets say that you wanted to sum all the numbers from 1 to 10. You could use a For loop where the loop control variable goes from 1 to 10 and in each iteration add the value of the loop control variable to a running total:
VB.NET:
Dim sum As Integer = 0

For i As Integer = 1 To 10
    sum += i
Next

MessageBox.Show(sum.ToString(), "Sum")
It is common to use i for a loop control variable although, particularly when you're learning, it's a good idea to use a more meaningful name if an appropriate one exists. If it represents an array index then name it index.
 
Hi,
I just started learning VB.NET last night as I was told it's one of the best programming languages to begin learning for someone wanting to be a programmer.
I'd have said C# is better for that role. VB.NET in its default configuration is sloppy.
 
I'd have said C# is better for that role. VB.NET in its default configuration is sloppy.
It depends what you mean by "best". VB.NET is easier for a rank beginner to pick up because the syntax is more natural and the fact that the language is less strict means you can learn the basic principles without having to worry about some of the details. That said, VB being less strict does provide it's own set of problems. "Best" is always a relative term. I think here it's being used as "easiest to learn the basics of", which is probably fairly accurate.
 
Hey, thanks for your explanation. I think I'm understanding it a bit more now, but at first it was For loops in general I was having trouble understanding.
Are they usually a confusing subject to learn at first?

And I've have only really touched Pascal and attempted Java before, and then heard that VB. NET was the best to learn if you have no previous history of this stuff, so I'm now giving VB .NET a try. :)
 
Are they usually a confusing subject to learn at first?
No, loops is usually the first a beginner learns, and the concept is one of the primary targets of computing; loops is used to repeat something, and computers has a high ability to make repetetive task easy for us, tell a computer what to do and it can do the same thing as many times you want without more effort from you. For-Each is just as common loop as For-Next, it is like saying "do as specified with one item.. and do the same with all other items". Other loops include Do/While which usually means keep doing something over and over until condition says stop.
 
Okay, thanks for the help. :) I think I'm beginning to get it now.

1. For i (name of loop's variable)
2. = 0 (where to begin from)
3. To *variable* (where to finish the loop and continue to next loop thereafter)

4. next i (loop again)

So, 1 gives the name of the loop, 2 tells VB where to begin from, 3 tells VB where it has to loop to until required loops have finished, if required loops have finished it avoids 4 because it's done what it has to, if not then 4 tells VB to loop the variable again.

Is that right?
 
if required loops have finished it avoids 4 because it's done what it has to, if not then 4 tells VB to loop the variable again.

Is that right?
Not exactly, the loop variable name after Next is optional, you can use it to better read when nesting loops. Next is the keyword that ends that loop, same as an If code block ends with "End If". VB is explicit about this to make it easier to read, C style languages for example just denote code blocks starting with a "{" and ending with a "}". So the the Next keyword just tells compiler that this code block was finished and it should do the next For until end condition.
 
Back
Top