Ask HN: Do you use goto, or labeled break/continue?

1 points by hugoj0s3 ↗ HN
C# 15 (in preview with .NET 11) lets break and continue target an outer loop by label:

outer: for (int x = 0; x < xMax; x++) {

   for (int y = 0; y < yMax; y++)
   {
       
       if (ShouldSkipRest(x, y))
          continue outer;

       if (ShouldExitAll(x, y))
          break outer;
   }
}

Honestly, "goto" never even comes to mind when I write an algorithm. I find it hard to follow, so when I need to leave nested loops I extract a method and "return" instead.

Do you use "goto" in real code? Do you think you'll use labeled break/continue?

https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-15#labeled-break-and-continue

4 comments

[ 0.20 ms ] story [ 10.1 ms ] thread
A few years back I was writing AVR8 assembly language and using JMP. I remember using goto in C a long time ago, like maybe the 1990s.

There are legit uses for goto in certain kinds of technical programming like interpreters but there are other ways to do it, similarly that example above is legit but if you are properly dividing your code up into subroutines you don't run into a lot of places where you'd want goto or labeled break/continue, especially in applications programming where you're not going to write sorts or numerical algorithms... Like I'd be likely to reach for it doing the kind of work that I'd be looking in an algorithms book.

(comment deleted)
I've never run into a case where I needed it. I was wondering if labeled break/continue could replace goto in those cases, or make the code cleaner.
It's better than goto in cases where it applies.