C# Goto

Statements execute one after another. With goto, though, we transfer control to a named label elsewhere in the method. Goto can be used to exit a deeply nested loop. As are many things, it is considered harmful.

Example

To start, we examine a program that uses goto to exit a nested loop. We can use code that does the same thing without goto. But this would require a flag variable (usually a bool) to check. Method syntax is another option.

In this example, method M contains three nested loops. The first loop iterates through numbers [0, 9], as do the two inner loops. But in the third loop, a condition is checked that causes the loop to exit using the break keyword.

This code uses a flag variable named "ok". It initializes the variable to true, but then sets it to false and breaks when the exit condition is reached. This avoids incrementing the dummy variable. The result is 50.

Next:Our goto example has the same effect. It detects the exit condition and uses goto to leave the inner loop. It has no flag variable.

Benchmark

I placed the above two methods in a benchmark program. I ran them in a loop for many iterations. The function result was checked each time. I tested them with the break first and the goto second, and the other way around.

So:The results were that goto performed worse when it was run first. Timings are shown next.

The behavior observed here shows that goto is sometimes not optimized as well with the JIT compiler. Goto is only slower when called in the first loop. This might mean that the JIT compiler does not work as well with it.

Therefore:With these methods, break is faster because it is more reliably JIT-optimized.

Switch

The goto statement also can be used in a switch block. The switch allows control to be transferred to another case in this way. Goto still transfers the flow of control, but causes somewhat less confusion in switch.

Tip:To look on the bright side, this can enhance performance and reduce code size in some cases.

Syntax:There is a special syntax for this construct: it requires goto case or goto default. We examine this syntax form.

Next steps. For IDs of 1000 or 100, the value returned from the method is equal to the base price times 10. This occurs after IDs of 1000 were processed. For all other values, the price that is returned is the base price, which is 5.

Benefits, switch

Quality code is structured into discrete methods. We can simply call the same method with different values from a switch. But in certain cases we may benefit from goto in switch when trying to reduce method size.

Research

Goto was part of the 1960s emergence of structural programming. In his famous text "Go To Statement Considered Harmful," Dijkstra observed how goto-statements make logic more complex, harder to follow.

And:New options, like methods, are clearer. With them, programs are less likely to deteriorate into unfathomable messes.

The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program.

Summary

Goto may look fast and convenient. But it can confuse the runtime and result in poorer performance. Thus goto is not only more complex for programmers. It is more difficult for the JIT compiler to optimize.