Breaking out of a nested loop

If I have a for loop which is nested within another, how can I efficiently come out of both loops (inner and outer) in the quickest possible way?

I don’t want to have to use a boolean and then have to say go to another method, but rather just to execute the first line of code after the outer loop.

What is a quick and nice way of going about this?

I was thinking that exceptions aren’t cheap/should only be thrown in a truly exceptional condition etc. Hence I don’t think this solution would be good from a performance perspective.

I don’t feel it it is right to take advantage of the newer features in .NET (anon methods) to do something which is pretty fundamental.

25 Answers
25

Leave a Comment