- 2069 Chaitra · 1 mark
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
gotoare difficult to debug and maintain; - any job done by
gotocan be done more clearly withfor,while,ifandbreak; - it makes testing and proving correctness harder.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- CT secondary should not be kept open · Electrical Machines, chapter 2
- DC series motor should not start on no-load · Electrical Machines, chapter 4