?
Solved

Loops

Posted on 2004-11-29
2
Medium Priority
?
499 Views
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
0
Comment
Question by:Barako2
2 Comments
 
LVL 9

Assisted Solution

by:Lacutah
Lacutah earned 675 total points
ID: 12701816
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
      messagebox.show(s)
next

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)
loop
Example 2:
dim i as integer = 5
do
    '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
do
    '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"
0
 
LVL 18

Accepted Solution

by:
armoghan earned 675 total points
ID: 12702597
Hi Barako2,
This is quite comprehensive
http://www.startvbdotnet.com/language/loops.aspx

Cheers!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

864 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