What is goto statement in C

  • goto is a keyword, by using this we can pass the control anywhere within the program.
  • A goto statement is an example of a jump statement, which is sometimes referred to as an unconditional jump statement in certain contexts.
  • goto keywords always required an identifier called a label.
  • When the goto statement is found then control of the program immediately transfers to the label where it then starts executing code.

 

goto Keyword Syntax

Below syntax used for goto statement in c programming.

goto label;
statement 1;
label: 
Statement 2;

 

Example of goto keyword


#include<stdio.h>
int main()
{
   int sum=0;
   for(int i = 0; i<=10; i++){
	sum = sum+i;
	if(i==10){
	   goto addition;
	}
   }

   addition:
   printf("%d", sum);

   return 0;
}

 

Program Explanation

In this example, we defined label as addition and if the current value of i is 10, then the goto statement will direct execution to the addition label. This is the reason why the total displays the sum of up the numbers to 10.