+ Reply to Thread
Results 1 to 2 of 2

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

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

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

    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
    1. Introduction and Installation
    2. Objects and Events
    3. Variables
    4. The basic data types
    5. Logical Operators
    6. Relational Operators
    7. If statements Then
    8. Arithmetical Operators
    9. Loops Part 1
    10. Arrays
    11. Loops Part 2
    12. Try Catch statements
    13. Subs and Functions
    14. Difference between Scopes
    15. Select Statements
    16. Multidimensional arrays
    17. Structures
    18. Classes
    19. Enumerations
    20. Advanced Comments
    21. Compiling Directives





    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:


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

    <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:



    Code:
    For i As Integer = 0 To 2
        MessageBox.Show(i)
    Next
    
    
    For i As Integer = 1 To 10 Step 3
        MessageBox.Show(i)
    Next

    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
    Next

    <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:


    Code:
    Dim anExample(2) As Integer
    anExample(0) = 61
    anExample(1) = 15
    anExample(2) = 87
    
    For Each element In anExample
        MessageBox.Show(element)
    Next

    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.

    Code:
    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)
        MessageBox.Show(Element)
    Next


    Now you have learned about all the different types of loops. We'll continue in the learning progress soon. Cya.
    Last edited by Vswe; 03-21-2010 at 03:32 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

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

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

    +rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 11-02-2009, 05:37 PM
  2. VB.NET from beginner to advanced programmer Part 3 - Variables
    By Vswe in forum Visual Basic Tutorials
    Replies: 3
    Last Post: 11-02-2009, 05:18 PM
  3. Replies: 1
    Last Post: 11-02-2009, 06:05 AM
  4. Replies: 2
    Last Post: 11-02-2009, 06:01 AM
  5. Replies: 1
    Last Post: 11-02-2009, 05:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts