Solved

How would I exit an outer for loop from within an inner for loop?

Posted on 2014-12-14
3
131 Views
Last Modified: 2014-12-14
I have the following:

For a = 0 to 10
   For b = 0 to 10
      If (some condition) then
         'do something
      else
         exit outer loop (a) 'this is what I can't figure out how to do.
      End if
   Next b
Next a

How do I exit the outer For loop (a) from within For loop (b)?
0
Comment
Question by:NevSoFly
  • 2
3 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
Comment Utility
Depends a bit

You can use Exit sub (or function) to exit completely from that point.
You can use GoTo to jump to a particular point later in the routine.
You can use a=10 and b=10 to meet the requirements for the loop to be completed and let the program just continue as if the loops had been fully completed
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 300 total points
Comment Utility
Having to force exit from for loops indicates a bad program design, there are other structures that are better for this purpose. The reason for this is that a For loop implies that it should be iterated a specific number of times. Use a While loop and a Boolean to jump out when necessary. I have assumed that you also what to immediately exit Loop B as well as loop A.



a=0
ExitA = False
While a<=10 AND ExitA = False
    b = 0
    While b <= 10 AND ExitA = False

       If (some condition) then
          'do something
       else
          ExitA = True
       End if
       b = b +1
    Exit While '  (or WEND) depends on version
    a = a + 1
Exit While '  (or WEND) depends on version

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>Having to force exit from for loops indicates a bad program design

I would say that is only partially correct.  There are times when a for loop does make sense eg. iterating an array, and one may want to exit the for loop before the end of the array is reached.
What is certain is that a for loop could be written as a while loop BUT that might be the same code in effect - just requiring more lines of code to be written.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

8 Experts available now in Live!

Get 1:1 Help Now