C goto Statement

C loops

In the C languagegotoThe statement allows unconditional transfer of control to a labeled statement within the same function.

Note:In any programming language, the use of the goto statement is not recommended, because it makes the control flow of the program difficult to trace, making the program hard to understand and hard to modify. Any program that uses the goto statement can be rewritten in a form that does not require the goto statement.

Syntax

In C languagegoto Syntax of statements:

goto label;
..
.
label: statement;

Here,labelIt can be any plain text except for C keywords, and it can be placed in a C program.gotoBefore or after the statement.

Flowchart

C goto 语句

Example

Example

#include <stdio.h> int main () { /*Local variable definition*/ int a = 10; /*do loop execution*/ LOOP:do { if( a == 15) { /*Skip iteration*/ a = a + 1; goto LOOP; } printf("Value of a: %d\n", a); a++; }while( a < 20 ); return 0; }

When the above code is compiled and executed, it produces the following results:

a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
a 的值: 16
a 的值: 17
a 的值: 18
a 的值: 19

C loops

other extensions