goto is keyword used to alter the flow of a program goto statement allows us to transfer control of the program to the specified label goto is a jump statement used to transfer program control unconditionally from one part of a function to another. Use of goto statement is highly discouraged in any programming language
The goto statement in C Programming is used to alter the flow of a program. When the compiler reaches the goto statement then it will jump unconditionally ( both forward and backward ) to the location specified in the goto statement (we called it as label).
The label specified after the goto statement in C Programming is the location where we place the statements to execute.
We have used the word unconditionally because there is no restriction on control transfer. You can transfer program control from one position to any position within a function.
Syntax of goto statement
goto label;
... .. ...
... .. ...
label:
statement;
Parts of goto statement
goto is divided in two parts, label definition and goto keyword
There is no restriction in order of goto and label. You are free to define a label anywhere inside a function. For example both the below examples are valid.
Label definition above goto statement
label1:
goto label1;
Label definition below goto statement
goto label2;
label2:
Flowchart of goto statement
Use of goto in programming
goto makes program less readable, error-prone and hard to find bugs. Due to the unconditional control transfer from one part to other part of a function. At one point, you as a programmer cannot tell how program control came to a certain point in your code. Therefore, most of the programmers avoid the usage of goto.
Important Note:
You should avoid usage of goto as much as possible. Try to replace goto statements with a combination of if...else, switch...case and loopping statements.
Consider below example of goto to demonstrate when and why you should avoid usage of goto.
#include <stdio.h>
int main()
{
int i=1;
start:
goto print;
print:
printf("%d ", i);
goto next;
increment:
i++;
goto print;
next:
if(i < 10)
goto increment;
else
goto exit;
printf("I cannot execute.");
exit:
return 0;
}
The above program is hard to read and find bugs if any. Hence, you must avoid usage of goto in such situations.
goto can be useful in many cases such as.
Example program to demonstrate goto statement
Many text and other materials on internet make use of goto as a loop. However, I believe goto should not be used as a replacement of loops. In below program I will show one real use of goto statement.
We can make use of goto to exit from a deeply nested loop.
/**
* C program to demonstrate usage of goto
*/
#include <stdio.h>
int main()
{
/* Variable declaration */
int i, j, k;
/* Some sample loop */
for(i=1; i<=10; i++)
{
for(j=1; j<=10; j++)
{
k = 1;
while(k<=10)
{
/* Some condition */
if(j==5 && k==5)
{
/* Move the program control outside the loop */
goto out_of_loop;
}
printf("%d ", k);
k++;
}
}
}
/* goto label */
out_of_loop:
return 0;
}