It is primarily used to transfer the control of execution to any place in a program. It is useful to provide branching within a loop. It is a well known as 'jumping statement.'
When the loops are deeply nested at that if an error occurs then it is difficult to get exited from such loops. Simple break statement cannot work here properly. In this situations, goto statement is used.
Syntax :
statement 1;
statement 2;
goto label;
statement 3;
statement 4;
label;
statement 5;
Example :
#include <stdio.h>
#include <conio.h>
void main()
{
int x=1, y;
clrscr();
while(x<=3)
{
for(y=1; y<=3; y++)
{
printf("\nHello");
if(y==2)
goto exit;
}
x = x + 1;
}
exit:
printf("\n\n Exited !");
getch();
}
Output :
Hello
Hello
Excited !
Description :
The for loop executes normaly two times dispalying 'Hello' but when the value of y becomes
2 then thegoto statement executes which takes the control to the label exit 'Exited'
isprinted.



0 comments:
Post a Comment