Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

VB.NET from beginner to advanced programmer Part 11 - Loops Part 2

VB.NET form loop

  • Please log in to reply
1 reply to this topic

#1 Vswe


    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 01 November 2009 - 05:20 PM

Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.

VB.NET from beginner to advanced programmer

Progressing further on we'll come to the two last types of loops: The For loop and the For Each loop. The reason I didn't wrote about them before was that I wanted to teach you about the arrays first.

For Loops:

The For loop will loop a certain amount of times and when it have looped so many times it will stop. When declaring a For loop you give it a name just like you do when you declares a variable. The For loops value(the number it's currently is on) can then be accessed by using that name exactly as you do with variables. This is the syntax for declaring a For loop:

For <loop_name> As <type> = <start> To <end> [Step <step>]
    'code inside the loop

<loop_name> is the name of the loop. You often use "i" as the name if you don't want to name it anything special.
<type> is the type the loop should be declared as.
<start> is at what value the loop should start at.
<end> is at what value the loop should end at.
[Step <step>] is optional. <step> is how much the value should change for each turn in the loop. The default value is 1.

Two examples on how to using the For loop, one is using the default value for the step9 the other one is not:

For i As Integer = 0 To 2

For i As Integer = 1 To 10 Step 3

In the first loop it will go from 0 to 2 with 1 as the step. This will result in an output of 0, 1, 2 and 3. The second one will go from 1 to 10 with a step of 3. Its output will be: 1, 4, 7 and 10.

For Each loop:

The For Each loop is a sort of For loop but its value will be the value of the element in an array at the current index, therefor the For Each loop is good to use to go through all elements in an array. Here comes the syntax:

For Each <loop_name> In <array>
'code inside the loop

<loop_name> is the name of the loop. It's this you will use to access the current element form the array.
<array> is the name of the array you want to go through.

Here comes an example on how to use the For Each loop to get all the elements in an array:

Dim anExample(2) As Integer
anExample(0) = 61
anExample(1) = 15
anExample(2) = 87

For Each element In anExample

In the above example the value of "element" will be 61 the first time, 15 the second and 87 the third since this is the three values we have stored in the array.

The above example can also be created with a For loop. It's better to use the For Each loop for it but I will now show you how it's done with the For loop so you can get a better idea how the For Each loop works.

Dim anExample(2) As Integer
anExample(0) = 61
anExample(1) = 15
anExample(2) = 87

For i As Integer = 0 To anExample.GetUpperBound(0)
    Dim element As Object = anExample(i)

Now you have learned about all the different types of loops. We'll continue in the learning progress soon. Cya.

Edited by Vswe, 21 March 2010 - 02:32 PM.

  • 1

#2 Guest_Jordan_*

  • Guest

Posted 02 November 2009 - 05:46 AM

How do you get the array key in a For Each using Visual Basic?

  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download