goto statement
A goto statement can transfer the control from current position to anywhere in the program. It is useful to provide branching within loops. Another use of goto statement is to exit from deeply nested loops where the break does not help. The general form of the goto statement is as follows:
while(conditional expression)
{
if(condition1)
goto stop;
statements;
statements;
statements;
statements;
statements;
if(condition2)
goto abc;
statements;
statements;
statements;
abc:
statements;
}
stop:
statements;
In different programming situations the goto statement is used to take the control anywhere we want.
The goto statement takes the control to the label specified with it. Any number of goto's can take the control to the same label. A program can contain more than one label but all the labels in a single program must be unique.
Example-
void main ( )
{
int a, b;
printf ("Enter two numbers");
scanf(""%d %d", &a, &b);
if (a > b)
goto large;
else
goto small;
large:
printf ("Largest no. = %d and smallest no. = %d", a, b);
small:
printf ("largest no. = %d and smallest no. = %d", b, a);
goto end:
end:
printf ("\n");
}