C has GOTO statement but one must ensure not to use too much of goto statement in their program because its functionality is limited and it is only recommended as a last resort if structured solutions are much more complicated. First let us see what the goto statement does, its syntax and functionality. The goto is a unconditional branching statement used to transfer control of the program from one statement to another.
Syntax of goto statement is:. Enter a number: 9. Enter a number: Table of Contents Syntax of goto Example: goto statement Reasons to avoid goto Should you use goto statement? Previous Tutorial:. Next Tutorial:. Share on:. Did you find this article helpful? Sorry about that. How can we improve it? Leave this field blank.
Related Tutorials. C Language Programs What is goto statement in C language,its advantages and disadvantages with program example. What is goto statement in C language,its advantages and disadvantages with program example. Introduction : In this article i will explain with a program what is goto or we can say labelled statement, what are its advantages and disadvantages and how to calculate sum of two numbers and prompting whether to continue with more calculations or not using goto statement.
Description : In previous articles i explained the program How to find greatest of two numbers using if else statement in C Language and Get number of days in month and year using else if ladder and Else if ladder to perform operation on two numbers based on operator and How to find greatest of two numbers using conditional operator.
In this program i am going to explain what goto statement is and how to use it. G oto statement is used to alter the normal sequence of program execution by transferring control of execution to some other part of the program unconditionally. So the control of flow transfers unconditionally to the statement specified by the label of goto statement. Syntax for goto:. Unfortunately, though, it only exits the current loop, which isn't helpful in the case where you have multiple loops nested within each other.
This is what it looks like with and without a goto. Note that the second example contains a special flag, just to indicate that you need to break from each loop. A better solution: Why not just move the looping code into its own subroutine? You may need to pass a handful of arguments, such as the contents of the loop, but it makes the code simpler. Now, to exit the loops, simply return from the subroutine.
A common use of GOTO is to perform a "common exit," or in other words always call a certain section of code before returning from a routine. Perhaps this is done to reset a variable or to close any file units opened in the routine. Maybe it does some other kind of cleanup before returning from the routine.
An option for how to not only perform common cleanup after exiting, but to guarantee that the cleanup will always be done is to create an "in-scope" object, which is cleaned up by IDL's automatic garbage collection. Basically, the idea is to put any cleanup code in a temporary object's ::Cleanup method.
0コメント