How to use Goto Statement in C language with example
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:
... .. ...
... .. ...
C Language goto statement example:-
printf("Enter the no");
printf("\n No. is greater than the 10");
printf("\n Enjoy the C Language.");
printf("No is smaller than 10");
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 🙂