+ Reply to Thread
Results 1 to 2 of 2

Thread: VB.NET from beginner to advanced programmer Part 9 - Loops Part 1

  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 9 - Loops Part 1

    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





    The Loops are block of codes that often will be run more then once at a row. Some of them will run a certain amount of time while others while run depending on the output of a condition. In this Part we will go through 3 of the 5 loops in VB.NET.






    The While Loop:


    The while loop will continue with its loop as long a condition remains True. The condition is tested at the beginning each time the loop will start a new turn. If the condition isn't met at the beginning the loop won't even run once. The syntax of a while loop looks as following:

    Code:
    While <condition>
         'Code that will be run once per turn
    End While

    To show a simple example:

    Code:
    Dim X as Integer = 3
    While X<300
    X*=2
    End While
    The above loop will loop while X<300, each time in the loop the value of X is doubled. After some turns X will be doubled from 192 to 383. Then when the loop check if the condition "X<300" is met it will get a False value and therefor the loop will end.


    You have to be sure you don't create a infinite loop. An infinite loop is a loop which will never end. The result is that the program will never continue and will stop to response. Here's an infinite loop:

    Code:
    Dim X As Integer = 141
    While X < 200
        X -= 5
    End While
    Independening on how many times the loop will loop, the loop won't end. The condition X<200 will always be True since X will only be smaller and smaller for each turn. The program will stop to response.





    Do While loop:

    This type of loop has only one difference to the While loop more then the syntax. The Do While loop will check the condition at the end instead of in the beginning. This is actually pretty important. Here's the syntax anyway:


    Code:
    Do
         'Code that will be run once per turn
    Loop While <condition>

    So why do we sometimes want to use the Do While loop? The reason is that sometimes we wants the loop to be run at least once even though the conditions aren't met.

    In the examples below I will use some more functionalities in the messagebox, I add a caption and what buttons it should use, I also check what button that was pressed to use it in an if statement:


    While Loop
    Code:
    Dim nPoints As Integer
    While nPoints >= 15
        nPoints -= 10
     
        If MessageBox.Show("Do you want any points?", "Points=" & nPoints, MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
             nPoints += 40
        End If
     
    End While
    Do While loop
    Code:
    Dim nPoints As Integer
    Do
        nPoints -= 10
     
        If MessageBox.Show("Do you want any points?", "Points=" & nPoints, MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            nPoints += 40
        End If
     
    Loop While nPoints >= 15

    Here, in both examples, the loop will continue as long as the user have 15 or more points, the user loses 10 points each time but can gain 40 new if he/she answer yes on the messagebox. But we want the user to be able to play this "game" so then the loop need to loop at least once. The while loop will test the condition in the beginning but since the user starts with 0 points the loop won't start at all. Because of that we use the Do While loop, then the code will run at least once so the "game" can start.




    Do Until loop:

    The Do Until loop as the Do While loop but with one exception. The Do While loop will continue if the condition are True and stop if the condition are False. The Do Until loop does the opposite, it will continue if the condition are False and stop if the condition are True. It will loop Until the condition are met. The syntax of the Do Until loop and also an example of it:

    Syntax:
    Code:
    Do
         'Code that will be run once per turn
    Loop Until <condition>
    Example:
    Code:
    Dim X As Integer = 312
    Do
        X /= 10
    Loop Until X = 0
    The loop above will loop until X is equals to 0, the it will stop.




    That was all for this time, we'll take the two last loops later on in the series.
    Last edited by Vswe; 03-21-2010 at 03:31 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 9 - Loops Part 1

    Not bad, +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:46 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