C ++ Questions about the Goto instruction

10

Studying programming I come across this strange instruction, in a certain way I understand that it jumps to the instruction it points to but why it is not used? The truth is a curious alternative to loops and conditional but I read that it is not good to use it, and my doubts are:

  • Why is not it used?

  • What happens in the program when it is used?

asked by Malthael 28.02.2017 в 16:09
source

4 answers

9

Why is not it used?

For the obfuscation of the code 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 use a break .

We can then check the code backwards , knowing that to reach an exact point, obligatorily has gone through the previous points (taking into account the if( ) ... else ... , of course).

This is not true in goto . If we find a tag, a destination for a goto , there is no way to know the sequence of instructions that got us there. Where we come from ? Above all, if we make our code a swatch of goto .

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, or anything bad implicit in using it, except as indicated in the previous paragraph. If you are in a block { ... } , the local variables to the block cease to exist and, if you are in C ++, the destructors are called correctly; everything works as expected.

    
answered by 28.02.2017 / 21:19
source
7

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 the use of the unconditional transfer instruction (GOTO), which could lead to " spaghetti code ", which is much more difficult to follow and maintain, and was the cause of many programming errors.

The spaghetti code is a pejorative term for computer programs that have a complex and incomprehensible flow control structure . Its name derives from the fact that this type of code seems to resemble a plate of spaghetti, that is, a bunch of intricate and knotted threads.

Traditionally this programming style is usually associated with basic and old languages , where the flow was controlled by very strong control statements like goto and using line numbers.

    
answered by 28.02.2017 в 21:04
4

Goto is an instruction that makes a jump in the program flow. As follows:

/*Bucle*/
int x = 0;
while(x < 100){
   if(x == 0) goto salto;
   x++;
}

salto: {
   printf("Ha sucedido un salto");
}

The while loop would not run 100 times, but in the first iteration, it would jump to the label indicated by goto.

In any case, this instruction is not recommended, it worsens the quality of the code and makes it more difficult to debug.

    
answered by 28.02.2017 в 20:54
1
  

Why is it not used?

Yes it is used; it is a perfectly valid C ++ instruction, and it is up to the programmer (and his environment) to decide whether or not to use it, according to what needs to be done and how he prefers to do it. It's like asking why is not break used? Why is not for? Why is not return used? It is the question that is not used, not goto.

  

What happens in the program when it is used?

An unconditional control transfer occurs to the instruction immediately following the associated label. Here is an example that seems appropriate: link

    
answered by 28.02.2017 в 22:08