Skip to main content

Why goto should be avoided

Short answer

goto should be avoided because it disrupts structured programming, creating tangled spaghetti code that is hard to read, debug, and maintain. Every goto can be replaced with clearer loops and conditional break/continue statements.

Explain why goto statement should be avoided.

goto jumps unconditionally to a labelled statement.

#include <stdio.h>
int main(void)
{
    int i = 1;
loop:
    if (i <= 3)
    {
        printf("%d ", i);
        i++;
        goto loop;      /* jumps back; same job as a while loop */
    }
    printf("\n");
    return 0;
}

It should be avoided because:

  • it breaks the structured flow, producing tangled "spaghetti code" that is hard to read and trace;
  • programs with goto are difficult to debug and maintain;
  • any job done by goto can be done more clearly with for, while, if and break;
  • it makes testing and proving correctness harder.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 5 questions →