Why is this an issue?
goto
is an unstructured control flow statement. It makes code less readable and maintainable. Structured control flow statements such
as if
, for
, while
, continue
or break
should be used instead.
Noncompliant code example
int i = 0;
loop:
printf("i = %d\n", i);
i++;
if (i < 10){
goto loop; // Noncompliant
}
Compliant solution
for (int i = 0; i < 10; i++) {
printf("i = %d\n", i);
}
Resources
- MISRA C:2004, 14.4 - The goto statement shall not be used.
- MISRA C:2012, 15.1 - The goto statement should not be used
- C++ Core Guidelines ES.76: Avoid
goto