Language: EN

programacion-goto

The GO-TO instruction

In the previous post, we saw that flow control is a fundamental part of any programming language, allowing us to control program execution and make decisions.

One of the first instructions used for flow control is the command GO-TO. The GO-TO command is an instruction that allows jumping from one section of the code to another, without executing the intermediate code.

That is, it allows changing the program’s execution flow. It is used to implement flow control structures, such as loops and conditionals, and also to jump to a specific section of the code based on some condition.

programacion-goto

The use of the GO-TO command is very simple. The reserved word GO-TO is used, followed by a label that identifies the section of the code to which you want to jump. The label is identified by a name and is placed in the section of the code that you want to skip.


// código que se ejecutará
goto etiqueta;
// todo este código nos lo saltamos

etiqueta:
// código al que se saltará

Currently, the GO-TO command is considered bad practice. It had its function in the past, but it generated difficult-to-maintain and manage code.

The problem is that, as the code increases in length and complexity, it becomes filled with “jumps here and there.” In the end, you end up with a maze, in which it is very easy not to know where the program will go.

programacion-goto-problem

As this was not very maintainable, very intelligent people started to think, what can we do to improve this? So the first thing they analyzed is in what circumstances do I have to do a GO-TO?

This gave rise to new ways and guidelines for structuring our code. In fact, this came to be called “programming without GO-TO”. This is where the control structures that we know today were born, which are mainly

  • Conditionals
  • Loops

In fact, most modern programming languages such as C++, C#, JavaScript, or Python, implement these structures. These structures provide greater clarity in the code, making it easier to understand and modify.

Although many languages still tend to maintain some type of GO-TO instruction, its use is not necessary and is strongly discouraged today.