?
Solved

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

Posted on 2014-12-14
3
Medium Priority
?
138 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 800 total points
ID: 40499018
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 1200 total points
ID: 40499079
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
ID: 40499107
>>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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 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