The syntax of goto statement is: goto label; Where label is a word representing statement or block of statements.
e.g.
output: printf("this is the output"); Here output is a lebel to printf() statement.
when the statement goto output is executed, the control is taken to the printf() statement having label output in the program.
Now lets consider following program
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter the number");
scanf("%d",&n);
if(n%2==0)
goto even;
else
goto odd;
even: printf("\nThe number is even");
odd: printf("\nThe number is odd");
getch();
}
"The number is even"
"The nuber is odd"
Which is certainly not expected. This occurs becuase goto just pass control to the given label and doesnot check any condition or case. Therefore it execute all statement following labeled statement. To avoid such a problem, simply use one another lebel say 'end'and use it as follow.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter the number");
scanf("%d",&n);
if(n%2==0)
goto even;
else
goto odd;
even: printf("\nThe number is even");goto end;
odd: printf("\nThe number is odd");goto end;
end:getch();
}

