goto statement in C

goto statement is useful to directly jump to a particular statement in the program. While executing the statements, suppose we want to directly jump from statement number 1 to statement number 10, We can use goto.

Syntax: goto LABEL;

Here, LABEL represents a string constant that points to a statement where the compiler should jump.

Example:     printf("\n Statement one ");
             printf("\n Statement two ");
             goto TEN;    */ jump from here */
             printf("\n Statement three");
             .
             .
             .
             TEN:   printf("\n Statement ten ");
             printf("\n Statement Tweleve");

In above example, statement one and statement two are executed. After that, goto TEN wil ask the compiler to jump to that statement, which is referenced by the label TEN. So, the compiler will skip statements three to nine and directly jump to statements ten. After that, statement Twelve is executed.

goto statements are not recommended in programming. The main reason is that they form infinite loops easily, which increases the complexity of a program.

POINT:    statement1;
          statement2;
          statement3;
          goto POINT;

The above code represents an infinite loop since the compiler jumps back to statement1 from the goto statement. Unless we use another statement to terminate this, it will repeatedly execute forever.

Naveed Tawargeri
 

Hi, I'm Naveed Tawargeri, and I'm the owner and creator of this blog. I'm a Software Developer with a passion for Programming.