Quiz Entry - updated: 2026.07.10
Why is goto considered harmful, and when might you actually use it?
Arbitrary goto jumps make control flow a tangle ("spaghetti code") — but one disciplined use survives: jumping forward to a single cleanup/error label.
Don't use goto like this:
c = 0, i = 0;
test:
if (i >= N) goto end;
c += a;
i++;
goto test;
end:
Use a loop instead:
for (i = 0; i < N; i++) {
c += a;
}
When goto IS acceptable - error cleanup:
// Without goto - deeply nested, repetitive cleanup
void foo(int a, int N) {
if (a) {
if (b) {
if (c) {
do_something();
} else undo_stuff();
} else undo_stuff();
} else undo_stuff();
}
// With goto - cleaner error handling
void foo(int a, int N) {
if (!a) goto error;
if (!b) goto error;
if (!c) goto error;
do_something();
return;
error:
undo_stuff();
}
The Linux kernel uses this pattern extensively for cleanup.
Go deeper:
Goto — Wikipedia — Dijkstra's critique, spaghetti code, and structured programming.