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?
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.
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.
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.
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.
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With