Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do While

Status
Not open for further replies.

job357

Technical User
Sep 16, 2002
106
US
Greetings, consider the following:

Inital = lstItem.Items.Count
Do While pmintInital < 100
pmintInital += 1
lstOrder.Items.Add("lstItem.Items")
Loop

I am trying to assign the contents from one Listbox to another via a Do While statement.

Thanks!
 
For i As Integer = 0 to lstItem.Items.Count - 1
lstOrder.Items.Add(lstItem.Items(i))
Next

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Man that was really quick! Thanks this is exactly what I was looking for.

 
Can anyone explain when to use Do While...Loop vs. Do...Loop Until? I understand posttest vs. pretest, but could I get a simple sample of why one would not work in the place of the other?
 

Code:
dim I as integer = 0

Do While I < 0
 I+=1
Loop
msgbox I
I will be 0

Code:
dim I as integer = 0
Do
 I+=1
Loop Until I < 0
msgbox I
I will be 1

The pre/post test is the critical difference. If you test before the code, you have the possibility of never executing the code inside the loop. If you test after the code, you will ALWAYS run the code atleast once.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks Rick for the expediant reply!

Here is the problem-

I am trying to teach students on repetitive statements, but haven't reached the chapters on listboxes, string manipulation or any sort of database technologies. They want a real world example of why they would choose the pretest over the posttest, and I am hitting a creativity road block! The book has plenty of exercises like the one you indicated , but they want more "meat" in there code.

Any thoughts would be great.
 
Here's an example for reading from a file: (note that this is more of a psuedo code type of example, not compilable)

Code:
file.open
do 
  file.readline
  'do something 
loop until file.EoF

Code:
file.open
file.readline
do while not file.EoF
  'do something 
  file.readline
loop

In the first example, if the file is empty the first loop will likely throw an exception when it attempts to do something with the line of the file that was just read in. The second example would read the line, see that the file is at the EoF and would not attempt to do anything with it.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top