goto Statement
The goto statement is used to alter the normal sequence of a C program.
The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.
The syntax of goto Statement:
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
The label is an identifier. When goto statement is encountered, control of the program jumps to label: and starts executing the code.
Example :
#include <stdio.h>
void main()
{
int number;
printf("Enter an integer number: ");
scanf("%d",&number);
if(number<=0)
goto end;
printf("Number is : %d", number);
end:
printf("Bye Bye !!!");
}
First run:
Enter an integer number: 123
Number is : 123
Bye Bye !!!
Second run:
Enter an integer number: 0
Bye Bye !!!
Post a Comment
If you have any doubts, Please let me know
Thanks!