i2tutorials

Goto statement

Goto statement

 

The goto statement unconditionally returns from one point to the next in the block. In other words, the goto statement transfers the execution to a different tag in the statement block.

The goto requires a label to identify where the branch is to be produced. A label is a valid identifier and should be followed with a colon. The label is positioned immediately prior to the transfer of the control statement. The goto statement can transfer control anywhere within a program.

 

Syntax:

 

goto label;
... .. ...
... .. ...
... .. ...
label:
statement;

 

The label is an identifier. When the goto statement is encountered, control of the program jumps to label: and starts executing the code.

 

Forward and backward jump:

 

goto

 

Example Program:

 

As shown in the above syntax, if the label is after the goto statement, then it is a forward jump and if the label is before the goto statement, it is called a backward jump.

 

void fun(int x)
{
back:
if( condition1)
{ 
x++;
goto next;
}
else if( condition 2)
{
x=x+2;
goto back;
}
x=x+3; 
next:
printf(“%d”,x); 
if (!done) 
goto back;
}

 

 

 

 

Exit mobile version