GOTO statement

 


We use the goto statement to get control at a specific point in a program. After we label the identifier we can send the control to backward or forward with the use of goto statement in the program, for that we have to use semicolon at the end. We have to keep the goto statement in condition so that it won't go to loop. The basic syntax of a goto statement is given below: 

 

Forward goto statement syntax   

 

goto label; 

…..  // skip this line  

…..   // skip this line  

….   // skip this line  

label; 

Statements; 

….. 

….. 

 

 

Backward goto statement syntax   

label; 

statements; 

…. 

…. 

 

goto label; 

 

 

Flowchart  

Program  


QUESTION:- goto statement to find even or odd number.

 


#include <stdio.h>
#include <conio.h>
void main()
{
    int no;
    char ans;
start:
    printf("enter a number: ");
    scanf("%d", &no);
    if (no % 2 == 0)
    {
        printf("even!\n");
    }
    else
    {
        printf("odd!\n");
    }
    printf("do you want to exit?: ");
    fflush(stdin);
    scanf("%c", &ans);
    if (ans == 'n' || ans == 'N')
    {
        goto start;
        printf("welcome back!");
    }
    else
    {
        printf("program execute!");
    }
    getch();
}

 

output  

enter a number: 7 

odd! 

do you want to exit?: n 

enter a number: 9 

odd! 

do you want to exit?: y 

program execute! 


Follow our page for regular updates.

0 Comments