The goto statement is used to alter the normal sequence of program instructions by transferring the control to some other portion of the program.
Syntax
goto label;
label : statement;
label is an identifier that is used to label the statement to which control will be transferred.
Example:
Program to print first 10 even numbers.
#include<stdio.h>
void main()
{
int i=2;
while(1)
{
printf(”%d “,i);
i=i+2;
if(i>=20)
goto outside;
}
outside : printf(”over”);
}
Output:
2 4 6 8 10 12 14 16 18 20 over
Applications
- To branch around statements under certain conditions in place of use of if-else statement.
- To jump the end of the loop under certain conditions by passing the rest of the statements inside the loop in place of continue statement.
- To jump out of the loop avoiding the use of break statement.
Note
- goto can never be used to jump into the loop from outside and it should be preferably used for forward jump.
- The usage of the goto keyword should be avoided as it usually violates the normal flow of execution.




Recent Comments