The Goto Statement in C





The goto statement is a control flow statement in the C programming language that allows transferring control to a labeled statement within the same function or block of code.


Syntax:


goto label;
// ...
label:
// Statement(s) to be executed when the goto is used


The goto statement is often considered discouraged and can lead to less readable and maintainable code. It should be used sparingly and carefully. When a goto statement is encountered, it transfers control to the labeled statement specified by label. The labeled statement is defined within the same function or block of code.



Goto statement is used to reach at a specific location means you can transfer control any where in the program.
  • we can reach at the begning of the program.
  • we can reach at the end of the program.


#include<stdio.h>
int main()
{
 int marks;
 label: printf("Enter the marks :\n");
 scanf("%d",&marks);
 if(marks<0 || marks>100)
   {
    printf("Not valid Marks :\n");
    goto label;
   }
   else
   {
   printf("Marks is : %d\n",marks);
   }
}
Output:
Enter the marks :
88
Marks is : 88