C goto statement


The goto statement is known as jump statement in C. As the name suggests, goto is used to transfer the program control to a predefined label. The goto statment can be used to repeat some part of the code for a particular condition. It can also be used to break the multiple loops which can't be done by using a single break statement. However, using goto is avoided these days since it makes the program less readable and complecated.

Syntax:



  1. label:   
  2. //some part of the code;   
  3. goto label; 


goto example

Let's see a simple example to use goto statement in C language.


  1. #include <stdio.h>  
  2. int main()   
  3. {  
  4.   int num,i=1;   
  5.   printf("Enter the number whose table you want to print?");   
  6.   scanf("%d",&num);  
  7.   table:   
  8.   printf("%d x %d = %d\n",num,i,num*i);  
  9.   i++;  
  10.   if(i<=10)  
  11.   goto table;    
  12. }  

Output:

Enter the number whose table you want to print?10
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100



Jay Kakadiya

Jay Kakadiya Creator

I am a computer field, & i am Web developer.

Suggested Creators

Jay Kakadiya