Goto statement considered useful
Try breaking out of nested while
loops without it.
In Python, we use exceptions:
class Done:
pass
try:
while foo:
while bar:
baz()
if time_to_exit:
raise Done
except Done:
pass
But in C, or C++ if we’re using an old compiler (or targetting a platform that doesn’t have good C++ support):
while (foo)
{
while (bar)
{
baz();
if (time_to_exit)
goto Done;
}
}
Done:
The goto
here has saved us a comparison, and actually makes the C code shorter than the Python code.