goto Statement in C
Notes:
goto Statement in C Programming Language:
goto statement in C:
goto statement transfers the control to the specified label
Note: a text followed by colon(:) symbol indicates a label
Ex: beginning: , end: etc.
Syntax:
goto labelText;
Ex 1:
Program to display first name, last name, full name and control which one to display using goto statement
goto fullName;
firstName:
printf("Manjunath\n");
goto end;
lastName:
printf("Chidre\n");
goto end;
fullName:
printf("Manjunath Chidre\n");
end:
Ex 2:
Program to display 1, 2, 3, 4, 5 using goto statement, as we display 1, 2, 3, 4, 5 using while loop
Program to display 1, 2, 3, 4, 5 using while loop
int i = 1;
while(i<6)
{
printf("%d\n", i);
i++;
}
Program to display 1, 2, 3, 4, 5 using goto statement, as we display 1, 2, 3, 4, 5 using while loop
int i = 1;
beginning:
if(i<6)
{
printf("%d\n",i);
i++;
goto beginning;
}
Note: if you do not understand goto statement and can’t handle it properly it is recommended not to use goto statement in your program