Studying programming I came across this strange instruction, in a certain way I understand that it jumps to the instruction it points to but why is it not used? The truth is a curious alternative to loops and conditionals but I have read that it is not good to use it, and my doubts are:
Why is it not used?
What happens in the program when it is used?
Why is it not used?
Because of the obfuscation of the code that it implies.
For example, in a loop (
do
,while
,for
), we can be sure that the only way out is in the loop itself; either because the necessary condition was met, or because we used abreak
.We can, therefore, check the code backwards , knowing that to reach an exact point, the previous points have necessarily been passed (taking into account the ,
if( ) ... else ...
of course).This is not fulfilled in a
goto
. If we find a label, a destination for agoto
, there is no way of knowing the sequence of instructions that got us there. Where we come from ? Above all, if we make our code a sampler ofgoto
.What happens in the program when it is used?
Exactly what you expect: you jump to the indicated label. There are no major side effects, nor anything bad implied in using it, except as indicated in the previous paragraph. If you're in a block
{ ... }
, variables local to the block no longer exist and, if you're in C++, destructors are called correctly; everything works as expected.Structured programming is a programming paradigm aimed at improving the clarity, quality and development time of a computer program, using only subroutines and three structures: sequence, selection (if and switch) and iteration (for and while loops), considering unnecessary and counterproductive use of the unconditional transfer (GOTO) instruction, which could lead to " spaghetti code ", which is much more difficult to follow and maintain, and was the cause of many programming errors.
Spaghetti code is a pejorative term for computer programs that have a complex and incomprehensible control flow structure . Its name derives from the fact that this type of code appears to resemble a plate of spaghetti, that is, a bunch of intricate and knotted threads.
This programming style is traditionally associated with basic and old languages , where the flow was controlled by very primitive control statements such as goto and using line numbers.
Goto is an instruction that makes a jump in the program flow. As follows:
The while loop would not execute 100 times, but on the first iteration, it would jump to the label indicated by goto.
However, this instruction is not highly recommended, it worsens the quality of the code and makes it difficult to debug.
If used; is a perfectly valid C++ statement, and it is up to the programmer (and their environment) to decide whether or not to use it, depending on what needs to be done and how they prefer to do it. It's like asking why don't you use break? Why isn't for used? Why is return not used? It is the question that is not used, not goto.
An unconditional transfer of control occurs to the instruction immediately following the associated "label". Here's an example that seems appropriate to me: http://en.cppreference.com/w/cpp/language/goto