The goto statement is a control flow statement that causes the CPU to jump to another spot in the code. This spot is identified through use of a statement label. The following is an example of a goto statement and statement label:
using namespace std;
tryAgain: // this is a statement label
cout << "Enter a non-negative number";
cin >> dX;
if (dX < 0.0)
goto tryAgain; // this is the goto statement
cout << "The sqrt of " << dX << " is " << sqrt(dX) << endl;
In this program, the user is asked to enter a non-negative number. However, if a negative number is entered, the program utilizes a goto statement to jump back to the tryAgain label. The user is then asked again to enter a new number. In this way, we can continually ask the user for input until he or she enters something valid.
In the section on variables, we covered three kinds of scope: local (block) scope, file scope, and global scope. Statement labels utilize a fourth kind of scope: function scope. The goto statement and it’s corresponding statement label must appear in the same function.
There are some restrictions on goto use. For example, you can’t jump forward over a variable that’s initialized in the same block as the goto:
goto skip; // invalid forward jump
int x = 5;
x += 3; // what would this even evaluate to?
In general, use of goto is shunned in C++ (and most other high level languages as well). Edsger W. Dijkstra, a noted computer scientist, laid out the case in a famous but difficult to read paper called Go To Statement Considered Harmful. Almost any program written using a goto statement can be more clearly written using loops. The primary problem with goto is that it allows a programmer to cause the point of execution to jump around the code arbitrarily. This creates what is not-so-affectionately known as spaghetti code. Spaghetti code has a path of execution that resembles a bowl of spaghetti (all tangled and twisted), making it extremely difficult to follow the logic of such code.
As Dijkstra says somewhat humorously, “the quality of programmers is a decreasing function of the density of go to statements in the programs they produce”.
Rule: Avoid use of goto unless necessary
|5.5 — While statements|
|5.3 — Switch statements|