1 What is the goto Statement? The goto statement is known as a jump statement in C language. It is used for an unconditional jump to another label, transferring control to another part of the program. The goto statement is generally used infrequently because it worsens the readability and complexity of the program.Syntax:
Example of goto Statement
Let’s look at a simple example demonstrating how to use the goto statement in C language. Open Visual Studio and create a project named: goto, and within this project, create a source file: goto-statement.c, with the following code.
Execute the above code to obtain the following result
2 Why is it so Unpopular? Over twenty years ago, when computer programming was still in its infancy, program flow was controlled by the “GOTO” statement. This type of statement allowed programmers to break the current line of code and jump directly to another different code segment. Below is a simple example using the GOTO statement:
Programming languages eventually began to introduce the concept of functions, allowing programs to break code. If completed, the goto statement is no longer used to indicate code breaks. After a function call, the function returns to the next instruction. List 2 provides an example. This practice improved program structure and enhanced readability. Since then, this has been regarded as the correct way to write programs. Whenever a software engineer sees or thinks of the goto statement, it instinctively causes aversion. One of the main reasons is that a program filled with goto statements makes it difficult to grasp the focus, complicating understanding and maintenance. Control flow with functions is shown below.
According to Wikipedia, the goto statement has always been a target of criticism and debate, with the main negative impact being that using the goto statement worsens program readability, even leading to unmaintainable “spaghetti code.” As structured programming became increasingly popular in the 1960s and 1970s, many computer scientists concluded that programs should always use commands known as “structured” control flow, such as loops and if-then-else statements, to replace GOTO. Even today, many programming style guidelines prohibit the use of goto statements. Defenders of the GOTO statement argue that limited use of the goto statement does not lead to low-quality code and claim that in many programming languages, some tasks cannot be directly implemented without one or more goto statements, such as implementing finite state machines, breaking out of nested loops, and exception handling. Perhaps the most famous criticism of GOTO is Edsger W. Dijkstra’s 1968 paper titled “Go To Statement Considered Harmful.” Dijkstra argued that unrestricted use of the goto statement should be abolished from high-level languages because it complicates the task of analyzing and verifying program correctness (especially involving loops). Another viewpoint appeared in Dijkstra’s “Structured Programming with Go To Statements,” which analyzed many common programming tasks and found that some of them would achieve the most ideal structure using GOTO. These criticisms have influenced the design of some programming languages. Although the designers of the Ada language recognized the criticisms of GOTO in the late 1970s, the statement was still included, primarily to support the automatic generation of code where goto statements are essential. However, labels as destinations for goto statements must be enclosed in double angle brackets (e.g., <<Start_Again>>), a syntax not used in other languages. This makes it easier to check for the existence of goto destinations in the program. The goto statement itself uses a simple form: goto Start_Again;.3 Variants of goto Many languages, such as C and Java, provide related control flow statements like break and continue, which are effectively restricted goto statements. Their function is to jump unconditionally, but they can only jump to the end of the loop block—either continuing to the next loop (continue) or ending the loop (break). The switch statement in C, C++, and Java efficiently implements a multi-way goto, where the jump target is selected based on the value of an expression. This also leads to the conclusion that there is no reason we must use goto. Regarding this, the current usage of goto is as follows: The result of the goto statement: The goto statement is retained in high-level programming languages like C/C++, but it is recommended to avoid or use it sparingly. In some newer high-level programming languages, such as Java, the goto statement is not provided; although it is designated as a keyword, its use is not supported, making programs concise and readable. Nevertheless, later versions like C# still support the goto statement, which has the advantage of ensuring a unique exit point in the program, avoiding overly large if nesting. On the other hand, the goto statement is merely discouraged, not prohibited. So when can the goto statement be used?4 Situations to Consider Using goto
- Directly breaking out of multiple loops;
- Cleaning up resources on error;
- Situations that can enhance program clarity.
Unrestricted use of goto: destroys clear program structure, worsens program readability, and can even lead to unmaintainable “spaghetti code.” It often brings errors or hidden dangers, such as potentially skipping the construction of certain objects, variable initialization, or important calculations. The following principles regarding the use of goto statements can be referenced by readers.
- Using the goto statement can only jump to the same function and cannot jump from one function to another.
- When using the goto statement within the same function, the starting point of the goto should be at the end of a small function segment, and the destination label should be at the beginning of another small function segment within the same function.
- It is not allowed to jump from a complex execution state to another position, such as breaking out of multiple nested loop conditions.
- Avoid two-way jumps. This is the easiest way to lead to “spaghetti code.”
Students who have read the Linux kernel code should notice that there are actually many places in the Linux kernel code that use the goto statement, but you will find that its use is very cautious and generally follows the principles mentioned above.