Introduction
Fundamentals
C Flow Control
C Functions
C Arrays
Goto Statement in C Language
Goto is a jumping statement in c language, which transfers the program's control from one statement to another statement (where the label is defined). In this tutorial, we will learn about goto statement in C Language.
The goto statement is a jump statement, often known as an unconditional jump statement in some situations. Within a function, you can jump from one place to another by using the goto statement.
Syntax of goto Statement:
goto label;
... .. ...
... .. ...
label:
statement;
C Language goto Statement Example:
#include<stdio.h>
void main()
{
int age;
printf("Enter you age:");
scanf("%d", &age);
if(age>=18)
goto g; //goto label g
else
goto s; //goto label s
g: //label name
printf("you are Eligible\n");
return;
s: //label name
printf("you are not Eligible");
}
Output :
Enter you age:12 you are not Eligible
Output :
Enter you age:23 you are Eligible
Disadvantages of using goto statement:
- It makes the program confusing, less readable and complex.
- when this is used, the control of the program won't be easy to trace.
- It makes testing and debugging difficult.
- Use of goto can be simply avoided using break and continue statements.