GOTO Statement in C Programming

GOTO Statement in C

  • the 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 Twelve");
  • In the above example, statement one and statement two are executed. After that goto, TEN will ask the compiler to jump to that statement which is refereed by the label TEN. So, the compiler will skip the statements three to nine and directly jump to statements ten. After that, the 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 always the compiler jumps back to statement1 from the goto statement. Unless we use another statement to terminate this, it will repeatedly execute forever.
naveed08st
 

Click Here to Leave a Comment Below 0 comments

Leave a Reply:

Select Language
Copy link
Powered by Social Snap