Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > exiting/ending a loop


Reply
 
Thread Tools Display Modes
  #1  
Old 02-17-2004, 05:01 AM
melbournemartin melbournemartin is offline
Freshman
 
Join Date: Oct 2003
Posts: 48
Default exiting/ending a loop


how would i be able to stop a for/next loop with an if statement?

basically saying

for x = 1 to 40
print examplearray(x)
if examplearray(x) = "example string" then
EXIT LOOP
next

realli the only thing that matters is the bit in caps

i tried this statement in the for next loop:

if something = something then
next

but that just came up with the "next without for" error

next is the only way i know to end a for/next loop but is there another way that i can exit it like halfway through the loop with an if statement or something

thnx a lot
Reply With Quote
  #2  
Old 02-17-2004, 05:04 AM
bk2003's Avatar
bk2003 bk2003 is offline
Contributor
 
Join Date: Aug 2003
Location: Gothenburg, Sweden
Posts: 648
Default

Exit For

(i'm almost sure) otherwise search MSDN
Reply With Quote
  #3  
Old 02-17-2004, 05:06 AM
malloc malloc is offline
Senior Contributor
 
Join Date: Feb 2004
Location: The land of Oz
Posts: 851
Default

You can exit a for loop with the Exit For statement.

example:
Code:
For i = 0 to 10 debug.print i if i=5 then exit for next i
Reply With Quote
  #4  
Old 02-17-2004, 05:16 AM
jumentous's Avatar
jumentous jumentous is offline
Contributor
 
Join Date: Sep 2003
Location: Australia
Posts: 612
Default

it depends on the loop type

in a For/Next Loop Using Exit For
in a do/loop Loop use Exit Do
__________________
"In The Land of The Blind the One-Eyed Man is King."

Jumentous
Reply With Quote
  #5  
Old 02-17-2004, 06:13 AM
Peperl's Avatar
Peperl Peperl is offline
Senior Contributor
 
Join Date: Apr 2003
Location: Cartagena, Spain
Posts: 1,371
Default

I don't know if this is what you refer, but you can use both bucles

Code:
For myforbucle = 1 to 10 do until condition = True Loop Next myforbucle

This executes a bucle ten times, but no increas myforbucle till the other bucle's condition is True
__________________
Pri Oh Persia
Reply With Quote
  #6  
Old 02-17-2004, 06:30 AM
bk2003's Avatar
bk2003 bk2003 is offline
Contributor
 
Join Date: Aug 2003
Location: Gothenburg, Sweden
Posts: 648
Default

WARNING, that loop is not going to end.

I'm sure the Exit For will work, maybe not the prefered way of ending an loop, but it will work.

A DO loop that tests for both end conditions is perhaps a better solution.

Code:
x=1 end_loop = false DO print examplearray(x) if examplearray(x) = "example string" then end_loop=true x=x+1 LOOP until (x=40) or (end_loop=true)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Standards and Practices loquin Tutors' Corner 10 07-28-2006 12:16 PM
Timing Loop Examples (DirectX 7) munsie DirectX 0 01-21-2004 02:50 AM
Do Loop Problem jmcbride Database and Reporting 1 09-17-2003 09:48 AM
vc++6: any1 know how to use threads eg _beginthread(), _endthread() funcs clockworkorange Miscellaneous Languages 2 06-09-2003 09:20 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->