There've been some useful counter-suggestions for this example, but I don't think that the general question can always be restructured away happily. E.g. I came here because my little parser method has a switch (state) inside of a while loop. Sure, I could use break if I converted all the switch cases to if...else if, but I'd much rather not. Moving the switch into a function works but requires passing lots of parameters (and diminishing performance?). I held my nose and went with goto. I think a boolean flag (plus continue to go to the while condition?) would be the other best option.
–
Jon CoombsSep 29 '13 at 2:07

Gets the accepted flag for adding in the example. Thanks all who responded.
–
Matthew VinesJun 11 '09 at 17:58

1

This is slightly different from my answer which was that you could keep both loops rolled into one function where here there are 2 functions, each containing just one loop.
–
JB KingJun 12 '09 at 13:37

I would say the easiest solution is Eric's LINQ one. However, assuming we're on .NET 2 and don't have LINQ, I would argue that moving the whole of the OP's original example into another method and using return to break out of the loops is much cleaner than splitting the loop over two methods as the two methods end up obscuring the intent.
–
Damian PowellSep 6 '11 at 10:49

Very good Eric, I like your thinking :-)
–
Doctor JonesJun 23 '09 at 10:52

But in this case I check all items. It isn't good because I need first one only.
–
RredCatJun 28 '11 at 7:54

5

@RredCat: I'm sorry, I don't understand your objection. Is your objection (1) that you need the first item that matches, not whether there is such an item or not? If so, then use "FirstOrDefault", rather than "Any". Or (2) that "Any" checks every item even if the first one works? Why do you believe that "Any" does that? It stops when it gets one that works.
–
Eric LippertJun 28 '11 at 13:41

+1 for goto. People today are scared of goto, but this is a legitimate use of one.
–
rlbondJun 11 '09 at 18:13

4

I made the mistake of mentioning goto for a similar problem and got downvotes. But using the break keyword is pretty much the same thing as calling a goto to the end of the loop, no?
–
Meta-KnightJun 11 '09 at 18:31

1

@Meta-Knight: while you're at it, multiple returns from a function is equivalent to a goto. its just less messy because it imposes the restriction that you can't do upwards goto, or jump over a goto label (both types degenerate quickly into chaos)
–
JimmyJun 11 '09 at 19:06

1

@Jimmy every control structure is equivalent to a goto. Similarly, every program you write in Python, Ruby, C#, or Lisp can be duplicated exactly in assembly code or raw machine code. Languages are constructs which bridge the gap between computing hardware and human developers, it's important to recognize that returning a value from a function is a design pattern which has much different implications in how it will be used than a simple goto, even if they share implementation similarities under the hood.
–
WedgeJun 11 '09 at 21:10

Beat me by a few seconds, but I wrote a nice example, so mine's staying!
–
mquanderJun 11 '09 at 17:53

1

Why the downvote? He is just saying what mquander suggests, but without a code example.
–
Matthew VinesJun 11 '09 at 17:54

1

this sort of method is not always completely viable, particularly if you have a complicated set of evaluations on your cells to do in the loop dependent upon other things in your method.
–
Paul SonierJun 11 '09 at 17:56

4

I thought I'd leave writing the code as an exercise for the person posing the question. :)
–
JB KingJun 11 '09 at 17:57

EDIT:
Just to add a little more warning here; it is generally considered bad practice to use goto's as they quickly can lead to spaghetti code that is (nearly) impossible to maintain. That being said, it was included in the C# language, and is available for use, so clearly there are people who believe it has valid usages. Know that the feature exists and use with great caution.

Even though I'd never use a goto myself, I was wondering the same thing... The answer by PeterAllenWebb got 2 upvotes for basically the same answer. This one's actually a better answer too (in terms of providing a usage example). Odd.
–
PwninsteinJun 11 '09 at 17:59

1

Improper use of goto is bad, this clearly isn't.
–
RobMar 5 '13 at 2:35

I was going to suggest this until I remembered xkcd.com/292 But yes, a goto could solve the problem.
–
ChetJun 11 '09 at 17:53

2

the fact it's used in MSDN doesn't mean it's the best way of doing things (as many other examples there), i'm still at the avoid gotos whenever you can point of view, this problem can be solved in other ways (primarily extracting the logic into the separate function and returning the function)
–
zappanJun 11 '09 at 17:58

The best way is to not do this. Seriously; if you want to find the first occurrence of something in your nested loops, and then finish looking, then what you want to do is NOT to examine each element, which is explicitly just what the foreach construct does. I'd recommend using a regular for loop with a termination flag in the loop invariant.

//describe how to find interesting cells
var query = from row in grid.Rows.OfType<DataGridViewRow>()
from cell in row.Cells.OfType<DataGridViewCell>()
where cell.Value == myValue
select cell;
//nab the first cell that matches, if any
DataGridViewCell theCell = query.FirstOrDefault();
//see if we got one
if (theCell != null)
{
//Do something with theCell
}