`goto` — C++ Keyword
`goto` — C++ Keyword
The goto keyword in C++: unconditional jump to a labeled statement in the same function.
`goto` — C++ Keyword
The goto keyword in C++: unconditional jump to a labeled statement in the same function.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
gotoUnconditionally transfers control to a labeled statement within the same function.
goto label;
// ...
label: statement
#include <print>
int main() {
int i = 0;
loop:
if (i < 5) {
std::print("{} ", i);
++i;
goto loop;
}
std::println();
// Output: 0 1 2 3 4
// Common legitimate use: exit nested loops
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 3; ++y) {
if (x == 1 && y == 1) goto done;
}
}
done:
std::println("exited nested loops");
}
goto cannot jump over a variable initialization into a scope where that variable is used.break, return, exceptions) in almost all cases.gotoint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}