There is one case for using goto when you have nested loops and you’d like to jump all the way out. The goto statement was very popular in BASIC but often resulted in programs being like spaghetti with gotos all over the place.
Here’s a contrived example where I have used a goto.
#include <stdio.h>
int array[10][10][10];
int main() {
// Populate array
for (int y=0;y<10;y++)
for (int x=0;x<10;x++)
for (int z=0;z<10;z++) {
if (array[x][y][z]==0)
goto exit;
}
exit:;
}
In my C programs, for-loops are the most popular followed by while-loops, do-while loops and gotos are hardly ever used at all.
(Visited 114 times, 2 visits today)