Abstract

A goto statement is used to branch (transfer control) to another location in a program.

Syntax

Syntax Diagrams

goto Statement Syntax Diagram

BNF

goto-statement
::= 'goto' <label>

EBNF

goto-statement
::= 'goto' <label>

Contextual Constraints

goto-statement
The goto statement can only be used within the body of a function definition.

Semantics

goto-statement
The goto statement transfers control to the labeled statement whose label matches the identifier.

Remarks

Example

#include <stdio.h>
#include <stdlib.h>

int main () {
  unsigned Count;
  unsigned x;

  Count = rand () % 7;
  printf ("The count is %i.  \nThis is ", Count);
  switch (Count) {
    case 0:
      printf ("none.\n");
      goto next;
    case 1:
      printf ("only one.\n");
      goto next;
    case 2:
      printf ("a pair.\n");
      goto next;
    case 3:
      printf ("three.\n");
      goto next;
    default:
      printf ("many.\n");
      goto next;
  }
  next:
  
  x = rand () % 5;
  printf ("The test number is %i.\n", x);
  while (x < 10) {
    ++x;
    if (x % 2 == 0) {
      goto done;
    }
    printf ("%i is an odd number.\n", x);
  }
  done:
  
  return 0;
}

Output


    

Reference Links

Break Statement <break-statement>
Continue Statement <continue-statement>

External Links

MSDN The C goto Statement

Other Languages

C Now
C GoTo Statement
C++ Now C++ GoTo Statement
C# Now C# GoTo Statement
COBOL Now COBOL GoTo Statement
FORTRAN Now FORTRAN GoTo Statement
Java Now Java GoTo Statement
JavaScript Now JavaScript GoTo Statement
Pascal Now Pascal GoTo Statement
Perl Now Perl GoTo Statement
PHP Now PHP GoTo Statement