C Syntax Conditional Statements goto

 

- Info:

- This tutorial shows how to use "goto" statement to jump to a labeld part of code.

- Goto is used to escape from multiple nested loops, or to go to an error handling exit at the end of a function.

- Labels have function scope.

- This tutorial is based on http://publications.gbdirect.co.uk/c_book/chapter3/flow_control.html.

 

- Example:

- Create C Console Application Test.c.

 

Test.c

#include <stdio.h>

int main() {

printf("Prepare to jump.\n");

goto mylabel;

printf("This is never executed.");

mylabel:

printf("Prepare jump to a label in another block.\n");

goto newlabel;

{

newlabel:

printf("Label in another block.");

}

return 1;

}