In the previous entry, we saw that flow control is a fundamental part of any programming language that allows us to control the execution of the program and make decisions.
One of the first instructions used for flow control is 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.
In other words, it allows changing the flow of execution of the program. 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.
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 to which you want to jump. The label is identified with a name and is placed in the section of code that you want to skip.
// code that will execute
goto label;
// all this code is skipped
label:
// code to which it will jump
Currently, the GO-TO command is considered a bad practice. It served its purpose 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 maze, where it’s very easy not to know where the program is going.
As this was not very maintainable, some very smart people started to think about what we could do to improve this. So the first thing they analyzed was, under what circumstances do I need to make a GO-TO?
With this, new ways and guidelines for structuring our code emerged. In fact, this came to be known as “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 type of GO-TO instruction, its use is currently unnecessary and is strongly discouraged.