Introduction
There has been a lot of hysteria about the 'goto' statement since the famous
computer scientist, Edsger Dijkstra, said in 1968 that he considered it to be
"harmful".
In those days, he had a point because the 'goto' statement produced a lot of
spaghetti code particularly by those using early versions of the BASIC
programming language.
Despite this warning, 'goto' was included in C, C++ and C#. It wasn't included
in Java though they do have labelled 'break' and 'continue' statements which
achieve much the same thing.
In C#, it's much tamer than in some other languages as you can't jump into a
nested block; you can only jump out of (or within) one. As far as the compiler
is concerned, it works quickly because it can be mapped directly to CIL, and
thence to native code, 'jump' instructions.
Although 'goto' isn't needed much in modern programming and it's always possible
to program around it, I believe its use is acceptable in certain scenarios which
I'd like to examine in this article. The scenarios presented are not necessarily
exhaustive.
Jumping between 'case' blocks in a 'switch' statement
Unlike C, C++ and Java, C# doesn't permit code to 'fall through' from one case
block to another unless the first case block is empty. This eliminates some
potentially hard to find bugs. If you do need to fall through, you can use the
'goto case' construct instead:
string
position = "first";
switch
(position)
{
case "first":
DoSomething();
goto case
"second"; // not allowed to 'fall through'
here
case "second":
DoSomethingElse();
break;
}
This is nice and clean and I don't think many
C# developers would quarrel with this usage.
Jumping out of (or within) deeply nested loops
The 'break' statement only jumps out of the current loop. If you want to exit
from two or more nested loops, then the usual solution is to use bool variables
in the following manner:
bool
finished = false;
for
(int i = 0; i < 10; i++)
{
while (someCondition)
{
// ....
if (someOtherCondition)
{
finished = true;
break;
}
}
if (finished) break;
}

Gowtham RajamanickamPosted Mar 13, 2015, 1:53 AM
Good article for learners....
Sam HobbsPosted Apr 5, 2011, 2:16 AM
I have had the job of untangling spaghetti code. I have untangled code that was much more of a mess than I hope anyone else reading this can imagine. The simple code that you show is not representative. The problem is that when a program with gotos is modified, it often gets more complicated and the complications can multiply. Imagine a long length of string. It gets tangled a little bit. So it can be untangled. What if it gets very tangled, and then it can easily get much more tangled. Sometimes string becomes so tangled that it is just thrown in the trash. I have seen code so complicated that most programmers could not successfuly modify the code. Absence of gotos does not guarantee clean code and I admit that anyone that writes good clean code can do it with gotos but the problem is that gotos can increase the complexity of programs faster when gotos are used by some people.