Team LiB   Previous Section   Next Section
goto statement Transfers execution to a labeled statement

statement := goto identifier ;

The goto statement transfers control to another statement in the same function. The identifier must match a statement label elsewhere in the function. Statement labels have the form identifier : statement.

Control cannot transfer into a try block. Transferring control into the middle of a block and across a declaration results in undefined behavior unless the declaration is for an uninitialized POD object.

Example

while (getline(cin, line))
  for (size_t i = 0; i < line.size(  ); ++i)
    if (line[i] == '.')
      goto exit; // Break out of nested loops.
exit:
...

See Also

break, continue, statement, Chapter 4

    Team LiB   Previous Section   Next Section