Re: How would you do this?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



MC wrote:
[...]
The idea is to do something like "break" but escaping immediately from two loops, not just one. Or should the data be handled differently, perhaps with an iterator that can find its way through a list of lists, so that this routine would only have one loop?

As "Joe Cool" says, there's at least one obvious approach as an alternative to using the "goto" statement.

But, as I've said before, I don't actually think that a "goto" statement is something to be avoided in and of itself. What should be avoided is all of the scenarios where the "goto" statement is used inappropriately.

Interestingly (to me, anyway), in Java there is no "goto" statement and the "break" and "continue" statements are specifically designed to allow for this kind of scenario. Each can take as an operand a statement label that indicates the program block that should be exited. In your example, it would look something like this (using a non-existing Java approach in a C# program):

FindList:
foreach (List<String> b in a)
{
foreach (string c in b)
{
// ...some computation...
if (/* some test */)
{
break FindList;
}
}
}

Similarly, the "continue" statement can continue a labeled loop block, rather than just the inner-most one.

In other words, Java has eliminated one of the most common legitimate scenarios for using a "goto" statement by simply offering the same basic functionality via statements without the same stigma attached.

Lest anyone come away from this thinking that Java designed this in an obviously-superior way than C#, consider that C# also uses the "goto" statement to implement fall-through "case" statements for a "switch". This forces the programmer to be explicit about falling through to the next switch, allowing the compiler to emit an error if they aren't. This particular C# feature helps prevent one of the more common bugs that show up in "switch" statements: accidentally falling through to the next "case" statement.

I admit, I kind of like the Java approach. It certainly wouldn't bother me to see C# adopt that syntax as a way to reduce the use of the "goto" statement even further. But, the "goto" statement in C# offers me the flexibility of deciding when it's appropriate to use, rather than the compiler simply taking that option away from me altogether. For me, that's a positive aspect of C#.

Anyway, that's a long way of saying that as far as I'm concerned, your example of a use of the "goto" statement isn't actually in need of fixing. :)

Pete
.



Relevant Pages

  • Re: ANN: pldev.org
    ... 200,000, all cases either end in break, or end in goto or return. ... noticed that instances of fall-through are commented as such; ... Some sort of 'continue' for switches or 're-switch' to call switch again is ... Alternately, you could place the switch in a loop, change the loop index ...
    (comp.lang.misc)
  • Re: Iteration in lisp
    ... state machine is just a tagbody and goto. ... But if you need to change the state-transition-matrix in the middle ... loop and either table-driven or separate-functions for defining the ... (tagbody state1 (trace-state1 aux) ...
    (comp.lang.lisp)
  • Re: COBOL aint quite dead - yet !
    ... of control there is nothing wrong with goto. ... The loop is entirely abstracted away. ... Then we have well-formed loops with a single invariant and a looping ...
    (comp.lang.cobol)
  • Re: QBasic to BBC BASIC translator updated
    ... constructs to if then plus goto statements. ... Although a compiler doesn't need to use a stack explicitly to ... to jump out of a loop is the issue of 'static scope'. ... of any other interpreted BASICs that work that way, ...
    (comp.lang.basic.misc)
  • Re: acceptable use of goto?
    ... loop, forward within the loop at various points, then forward out of ... Then you give every statement a label. ... No goto statements. ... Then I build a framework (of if or switch statements) to eliminate the ...
    (comp.lang.c)