Skip to content

C program to illustrate the use of goto statement

by yashan on February 5th, 2012

Definition of goto statement – In C programming language goto statement is used to transfer the control unconditionally from the one statement to any other statement in the program.  The format of the goto statement is as follows-

      goto label;

Below is the program in the C programming language to show the use of the goto statement.

/* C program to print the number upto 100 using goto statement */
#include<stdio.h>
void main()
{
     int n=0;
     
     a: n=n+1; /* 'a:' is the label */
     printf("\t %d",n);
     if(n>=100)
     {
               goto b;
     }
     goto a; /* Will go to the 'a' label */
     b: printf("\n End of the program"); /* 'b' label */
     getch();
}
               

Most of the times goto statement is used within if-else statement. In the above program we used the goto statement to print the counting up to 100. So when the last number is printed the if statement gets true and the goto statement in the if statement transfers the control at the end of the program.

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS