Posted on 2004-11-29
Last Modified: 2010-04-23
What are the advantages and disadvantages of these Loops:
* For Next Loops
*Do While Loops
* Do Until Loops
Please show code usage examples
Question by:Barako2
    LVL 9

    Assisted Solution

    The for loop is great for iterations of items in a collection, as a matter of fact, the For Each / Next will work with any collection that exposes an Array.GetEnumerator method.

    As an example: For a collection of strings (a string array) called MyStrings(), we can enumerate through each string...
    Dim s as string
    for each s in MyStrings


    Do While and Do Until loops are essential the same - although they are the inverse of eachother.  They both test a condition, either WHILE a condition is true or UNTIL a condition is met.  Both can also have the test at the "end" of the loop, ensuring that the loop will run at least once.

    To preform a task 5 times, you might use either of the following:
    dim i as integer = 5
    Example 1:
    do while i > 0
         'Decrement i...
         i -= 1
         console.writeline("{0} times left...", i)
    Example 2:
    dim i as integer = 5
        'Decrement i...
         i -= 1
         console.writeline("{0} times left...", i)
    while i > 0

    Having the conditional at the end of the loop means it will always execute at least once.
    dim i as integer = 0
        'Decrement i...
         i -= 1
         console.writeline("{0} times left...", i)
    loop while i > 0 'i = -1, but already went through and printed out "-1 times left"
    LVL 18

    Accepted Solution

    Hi Barako2,
    This is quite comprehensive


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now