Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I 'continue' a ForEach loop from a nested method?

Tags:

c#

c#-4.0

I have a ForEach loop that processes a rather large list of contacts. Instead of doing a bunch of processing in this main loop, I call a method which in turn calls a method, etc. Methods call other methods in other classes, maybe other namespaces.

How do I break from the current method if a condition is detected and I want to move on to the next contact? I'm in a method a few levels from the main ForEach loop.

Typically, you could use the continue keyword inside a ForEach to skip to the next element in the collection. continue is not an option in this case. When I type continue, it gets underlined in red with a "Unresolved Message" comment.

So, what do I do?

like image 595
DenaliHardtail Avatar asked May 05 '11 21:05

DenaliHardtail


People also ask

How do you continue a foreach loop?

To continue in a JavaScript forEach loop you can't use the continue statement because you will get an error. Instead you will need to use the return statement in place of the continue statement because it will act in the same way when using a forEach because you pass in a callback into the forEach statement.

How do I break out of nested foreach?

The only way to this directly is with a goto . Another (better) option is to restructure until the problem goes away. For instance by putting the inner code (while + foreach) in a method and use return to get back.

Can you nest foreach loops?

The nesting operator: %:% I call this the nesting operator because it is used to create nested foreach loops. Like the %do% and %dopar% operators, it is a binary operator, but it operates on two foreach objects. It also returns a foreach object, which is essentially a special merger of its operands.

Can we use nested for-each loop in Java?

Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove.


2 Answers

You're going down a bad path here; take a step back and reconsider your design.

In general it is a really bad idea to have methods that attempt to influence the control flow of their callers. A method is the servant of the caller, not the master. The method doesn't decide what the caller does next; that's not its business. Rather, a method:

  • performs a calculation and returns a result, or
  • modifies some state, and then
  • throws an exception if the operation could not be completed successfully

There are advanced control flow styles in which callees work together with callers to determine "what happens next" - Continuation Passing Style, for instance. But you shouldn't go there. They are very difficult to understand.

like image 186
Eric Lippert Avatar answered Sep 22 '22 21:09

Eric Lippert


Your supporting methods should probably return a value which is checked in the for-loop of the main method, and then continue from there if that's what the value indicates.

Using exceptions is another alternative, but they're generally considered slower-- I'm not sure about C# in particular. They're also usually considered bad form when used in this manner. Exceptions should be thrown in exceptional situations, not as a normal part of flow control. There are arguably situations where it's ok to use them in this manner, such as the web framework Play!, but you're probably not in one of them.

like image 41
Brad Mace Avatar answered Sep 24 '22 21:09

Brad Mace