Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C goto Statement

C goto Statement

C Loops C Loops

In C, the goto 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 discouraged. Because it makes the program's control flow hard to track, and the program hard to understand and modify. Any program using the goto statement can be rewritten without needing the goto statement.

Syntax

The syntax of the **goto ** statement in C is:



goto label;

..

.

label: statement;

Here,label can be any plain text except C keywords; it can be placed before or after a goto statement in a C program.

Flowchart

C goto Statement

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 code above is compiled and executed, it produces the following result:



a 的值: 10

a 的值: 11

a 的值: 12

a 的值: 13

a 的值: 14

a 的值: 16

a 的值: 17

a 的值: 18

a 的值: 19

C Loops C Loops