C program to illustrate the use of goto statement

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.

yashan has written 69 articles

2 thoughts on “C program to illustrate the use of goto statement

  1. Saptoparno Sinha Roy says:

    Write a program to print any name for 10 times using goto statement.

    #include
    #include
    Void main( )
    {
    int i=1;
    start:
    printf(“George\n”);
    i++;
    if (i<=10)
    goto start;
    getch( );
    }

    OUTPUT: –

    George
    George
    George
    George
    George
    George
    George
    George
    George
    George

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>