Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > VB.NET > VB.NET General Discussion

VB.NET General Discussion VB.NET general discussion area

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-01-2008, 2:43 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.0 (VS 2005/2008)
 
Join Date: Dec 2008
Age: 19
Posts: 10
Reputation: 18
Hb_Kai is on a distinguished programming path ahead
Default For loops

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-01-2008, 3:32 PM
JuggaloBrotha's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Jun 2004
Location: Lansing, MI; USA
Age: 25
Posts: 3,618
Reputation: 396
JuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NET
Default

What exactly is confusing you? Is it For Loops in general or something more specific?
__________________
Currently using: VS 2005 & 2008 Pro w/sp1 on Win7 Ultimate x64.


There are 3 kinds of people in the world: Those who can count and those who can't.
4 out of 3 people have trouble with fractions.

Windows has a 64 bit GUI for a set of 32 bit extensions on a 16 bit shell for an 8 bit OS using a 4 bit kernel made by a 2 bit company that can't stand 1 bit of competition.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-02-2008, 4:35 AM
jmcilhinney's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Aug 2004
Location: Sydney, Australia
Age: 40
Posts: 6,100
Reputation: 541
jmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalist
Default

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:
Code:
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-02-2008, 10:45 AM
cjard's Avatar
VB.NET Forum All-Mighty
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Apr 2006
Age: 65
Posts: 6,442
Reputation: 807
cjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond repute
Default

Quote:
Originally Posted by Hb_Kai View Post
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.
__________________
DW1 DW2 DW3 DW4 DNU PQ
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-02-2008, 4:46 PM
jmcilhinney's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Aug 2004
Location: Sydney, Australia
Age: 40
Posts: 6,100
Reputation: 541
jmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalist
Default

Quote:
Originally Posted by cjard View Post
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 12-03-2008, 7:46 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.0 (VS 2005/2008)
 
Join Date: Dec 2008
Age: 19
Posts: 10
Reputation: 18
Hb_Kai is on a distinguished programming path ahead
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 12-03-2008, 8:06 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,303
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

Quote:
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.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 12-03-2008, 6:42 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.0 (VS 2005/2008)
 
Join Date: Dec 2008
Age: 19
Posts: 10
Reputation: 18
Hb_Kai is on a distinguished programming path ahead
Default

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 12-03-2008, 6:58 PM
VB.NET Forum Idol
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2008
Location: USA
Posts: 866
Reputation: 499
MattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond repute
Default

It may be beneficial to take a look here: Loops in VB .NET - For Loop, While Loop, Do Loop
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 12-03-2008, 7:51 PM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,303
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

Quote:
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.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 3:02 PM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.