programacion-goto

The GO-TO instruction

  • 2 min

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

One of the first instructions used for flow control was the GO-TO command. The GO-TO command is an instruction that allows jumping from one section of 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 code based on some condition.

programacion-goto

GO-TO allowed jumping to another instruction

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


// code that will execute
goto label;
// all this code is skipped

label:
// code to which it will jump

Copied!

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

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 labyrinth where it is very easy not to know where the program will go.

programacion-goto-problem

The mother of all spaghetti code

Since this wasn’t very maintainable, some very intelligent people started thinking: what can we do to improve this? So the first thing they analyzed was: in what circumstances do I have to use a GO-TO?

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

  • Conditionals
  • Loops

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

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