C++ goto Keyword
Example
Use goto to jump back and print the numbers 1 to 3:
int i = 1;
start:
cout << i << "\n";
i++;
if (i <= 3) {
goto start;
}
Definition and Usage
The goto keyword jumps to another line of code in the same function. The line is marked with a label, which is a name followed by a colon, like start: in the example.
Code with goto is hard to read and follow, so most programmers avoid it. Use loops, break and continue instead.
Related Pages
Read more about loops in our C++ While Loop Tutorial, and about break in our C++ Break and Continue Tutorial.