First - and with focus on 'C' - there are valid, useful applications of the 'goto', which unconditionally jumps to a different location in our code (within its enclosing function body). However, in general, programmers tend to avoid using 'goto', because it can give rise to "spaghetti code". That is, programming that twists and turns and is entangled in a difficult-to-understand program flow.
![]() |
(If our program goes to... la-la-land, try the "ctrl-c" keys.) |
/* pgm16 source */
#include <stdio.h>
int main(void) {
int x, y, z; // all 3 variables are of type INT
x = y = z = 0; // ...and they're all initialized to 0
printf("\nProgram begins.\n\n");
here1:; // a named destination in our code
for (x = 0; x < 25; x++) {
z++;
if (x == 13)
goto here2;
else
y--;
here3:;
}
z -= 10;
while (y < 40) {
if (y == 35)
goto here1;
else if (z > 5)
goto here3;
y++;
z++;
}
here2:;
if (z < 30)
goto here1;
else
goto here3;
printf("\nProgram ends.\n\n");
return(0);
}