goto statement in C ?


The sequential statement are executed one after the other but if the programmer wants to transfer the control from one point to some other point in the program the goto statement can be used during this statement control can be transferred from one statement to the specified statement without any condition.

Syntax:-

goto label;

where,goto is a keyword and label is any identifier ending with semicolon( ; )

Note: "The label should be used along with a statement to which the control is transferred."

Q.) W.A.P to print sum of 1 to 10
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int sum=0,i=0;
 top:
 if(i>10)		  
 {
 goto end;
 }
 sum=sum+i;
 i++;
 goto top;
 end: printf(“%d”,sum);
 getch();
 }
Output
55