A goto statement causes your program to unconditionally transfer control to the statement associated with the label specified on the goto statement.
goto statement syntax >>-goto--label_identifier--;-----------------------------------><
Because the goto statement can interfere with the normal sequence of processing, it makes a program more difficult to read and maintain. Often, a break statement, a continue statement, or a function call can eliminate the need for a goto statement.
If an active block is exited using a goto statement, any local variables are destroyed when control is transferred from that block.
You cannot use a goto statement to jump over initializations.
A goto statement is allowed to jump within the scope of a variable length array, but not past any declarations of objects with variably modified types.
The following example shows a goto statement that is used to jump out of a nested loop. This function could be written without using a goto statement.
/**
** This example shows a goto statement that is used to
** jump out of a nested loop.
**/
#include <stdio.h>
void display(int matrix[3][3]);
int main(void)
{
int matrix[3][3]= {1,2,3,4,5,2,8,9,10};
display(matrix);
return(0);
}
void display(int matrix[3][3])
{
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
if ( (matrix[i][j] < 1) || (matrix[i][j] > 6) )
goto out_of_bounds;
printf("matrix[%d][%d] = %d\n", i, j, matrix[i][j]);
}
return;
out_of_bounds: printf("number must be 1 through 6\n");
}
Related information