goto statement in C

goto statement in C

By using goto statement you can transfer the control of program anywhere. This keyword is not recommended to use in any situation. 

Syntax


goto label;
. . . . 
. . . . 
label: statement;

Flowchart of goto statement in C
Flowchart of goto statement in C – Image Source

Lets make one program to understand it.

#include<stdio.h>

void main()
{
 int i=1;

 if(i==1)
  goto one;
 else if(i==2)
  printf("2");
 else
  printf("Its nothing");

 one:
 printf("Avoid goto keyword");
}

Output


goto statement in C

Why you should avoid goto statement in C?
It takes the control of the program anywhere and anytime which is not good for software development life cycle. In this cycle a program has to debug and re-test a lot of times. And with the use of goto keyword it makes very hard to debug the program.

This keyword is generally used to take the control outside the loop. That’s it. Otherwise you should avoid this keyword.

Leave a Comment

Your email address will not be published. Required fields are marked *