Contents
Why we should not use goto statement?
NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them.
Under what conditions might the goto statement be helpful?
The goto statement can be used to alter the flow of control in a program. Although the goto statement can be used to create loops with finite repetition times, use of other loop structures such as for, while, and do while is recommended. The use of the goto statement requires a label to be defined in the program.
Why is goto hated?
Using a goto to jump out of a deeply-nested loop can often be cleaner than using a condition variable and checking it on every level. Using goto to implement subroutines is the main way it is abused. This creates so-called “spaghetti code” that is unnecessarily difficult to read and maintain.
When do you use goto statement in a program?
A goto statement, on the other hand, tends to correspond to a concept in the running program, not in the problem domain. It says to continue execution at a specified point in the program. Someone reading the code has to infer what that means with respect to the problem domain.
Why is it bad to use goto in compiler?
The main reason goto has been controversial is because of its ill effects that arise when the compiler lets you use it too liberally. Problems can arise, for example, if it lets you use goto in such a way that you can now access an uninitialised variable, or worse, to jump into another method and mess with the call stack.
Is it ever worthwhile to use goto?
It doesn’t fit into the normal definition of “acceptable” goto use, but the function ended up significantly smaller and clearer as a result. To avoid the goto would have required a particularly messy violation of DRY. I think this whole issue has been a case of barking up the wrong tree.