Table of Contents

    Understanding the goto Statement in C: Usage and Examples

    Understanding the goto Statement in C: Usage and Examples

    Executing a goto statement causes a direct branch to be made to a specified point in the program. To identify where in the program the branch is to be made, a label is needed. A label is a name formed with the same rules as variable names; it must be immediately followed by a colon. The label is placed directly before the statement to which the branch is to be made and must appear in the same function or method as the goto.

    Syntax of goto statement

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

    Example of goto Statement

    
    #include<stdio.h>
    void main()
    {
    int goals ;
    printf("Enter the number of goals scored against India ");
    scanf("%d", &goals) ;
    if (goals <= 5)
    	goto atnyla ;
    else
    {
    	printf( "About time soccer players learnt C\n");
    	printf( "and said goodbye! adieu! to soccer \n");
    	exit() ; /* terminates program execution */
    }
    atnyla :
    	printf("To err is human! \n");
    }
    
    Output 1
    Enter the number of goals scored against India 4
    To err is human!
    Press any key to continue . . .
    
    Output 2
    Enter the number of goals scored against India 8
    About time soccer players learnt C
    and said goodbye! adieu! to soccer
    Press any key to continue . . .


    The only programming situation in favour of using goto is when we want to take the control out of the loop that is contained in several other loops. The following program illustrates this.

    
    #include<stdio.h>
    void main( )
    {
    int i, j, k ;
    for ( i = 1 ; i <= 3 ; i++ )
    	{
    		for ( j = 1 ; j <= 3 ; j++ )
    		{
    		for ( k = 1 ; k <= 3 ; k++ )
    		{
    			if ( i == 3 && j == 3 && k == 3 )
    			goto out ;
    			else
    			printf ( "%d %d %d\n", i, j, k ) ;
    		}
    	}
    }
    out :
    printf ( "Out of the loop at last! \n" ) ;
    }
    
    1 1 1
    1 1 2
    1 1 3
    1 2 1
    1 2 2
    1 2 3
    1 3 1
    1 3 2
    1 3 3
    2 1 1
    2 1 2
    2 1 3
    2 2 1
    2 2 2
    2 2 3
    2 3 1
    2 3 2
    2 3 3
    3 1 1
    3 1 2
    3 1 3
    3 2 1
    3 2 2
    3 2 3
    3 3 1
    3 3 2
    Out of the loop at last!
    Press any key to continue . . .

    Example of goto Statement

    
    
    // Program to calculate the sum and average of maximum of 5 numbers
    /* If user enters negative number, the sum and average of previously
     entered positive number is displayed */
    
    # include<stdio.h>
    
    int main()
    {
    
        const int maxInput = 5;
        int i;
        double number, average, sum=0.0;
        
        for(i=1; i<=maxInput; ++i)
        {
            printf("%d. Enter a number: ", i);
            scanf("%lf",&number);
    
        // If user enters negative number, flow of program moves to label jump
            if(number < 0.0)
                goto jump;
    
            sum += number; // sum = sum+number;
        }
    
        jump:
    
        average=sum/(i-1);
        printf("Sum = %.2f\n", sum);
        printf("Average = %.2f \n", average);
    
        return 0;
    }
    
    1. Enter a number: 5
    2. Enter a number: 4
    3. Enter a number: 2.1
    4. Enter a number: 1.6
    5. Enter a number: -2.6
    Sum = 12.70
    Average = 3.17
    Press any key to continue . . .

    Example of goto Statement

    
     
    // even or not using goto statement
    #include <stdio.h> 
    // function to check even or not
    void checkEvenOrNot(int num)
    {
        if (num % 2 == 0)
            goto even; // jump to even
        else
            goto odd; // jump to odd
     
    even:
        printf("%d is even \n",num);
        return; // return if even
    odd:
        printf("%d is odd\n",num);
    }
     
    // Driver program to test above function
    int main()
    {
        int num = 26;
        checkEvenOrNot(num);
        return 0;
    }
    
    26 is even
    Press any key to continue . . .