In this tutorial you are going to learn how to use effectively goto statement in C Language with the help of an example.

In C programming language goto is a special type of statement which we can use when we want to transfer the control of execution at some another label.

The goto statement required a label in order to identify the place where the branch is to be transfered.

This Label can be any valid variable name. And this Label must be followed by a colon and it is placed just before the statement where the control of goto is to be transferred.

Syntax of goto:


1
2
3
4
5
goto label;
... .. ...
... .. ...
label:
statement;

C Language goto statement example:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int n;
printf("Enter the no");
scanf("%d", &n);
if(n<=10)
goto no;
else
{
printf("\n No. is greater than the 10");
printf("\n Enjoy the C Language.");
exit(0);
}
no:
printf("No is smaller than 10");
getch();
return 0;
}

output :

Let see the explanation of the above goto C Program.

The above Program is a basic example to clear the concept of c go to statement. In the above program, you can see that there is a variable n of integer type. Now, what the above program will do?

It will check the condition with the help of if statement. If the condition will satisfy then it will transfer the control at label no as the property of goto statement of C and logic of the above program.

Now when we will give any input less then or equal to 10 then it will satisfy the if condition. And if the condition will satisfy then if body will execute and control will transfer to the label “no”.

Now after insertion in the body it will transfer to label no as mentioned goto no.

So it will print “no is smaller than 10” as output. That is all about the goto statement in the C programming language. Basically it will used by the programmers very rarely and also this statement is not that much popular as per real world solutions.

Now I hope your concept is now cleared 🙂