goto keyword in C Language

goto keyword is generally used as an alternate of loops in C Language means you can execute same lines of code with the help of goto keyword

In C Programming Language  we can directly jump on the place from where we want execution and for this we have to create a Label first and then at the place of goto keyword we mention the name of Label so that processor will jump on particular Label for execution.   

Point to Remember:- To create a program using goto keyword you must mention one condition before program execution so that program will terminate when condition is true otherwise goto statement always execute same lines of code infinity times as infinity loops.
Let us understand goto keyword deeply.

Syntax of goto keyword

Label:
line_1 of code;
line_2 of code;
............
line_n of code;
goto Label;               //at this time processor find the Label in code and jump to that Label.

Let us take an example for better understanding over goto keyword.

//Program to understand goto keyword in C Bhasha Gyan

#inlcude<stdio.h> 

int main()
{

ABC:                 // here Label name is ABC

printf("Hello goto keyword in C Bhasha Gyan");

goto ABC;         // at this point processor will jump on ABC Label and execute print statement again

return 0;

}

if you run above program you face a problem that is "printing statement executes infinite times" which is not a good practice in programming to stop this activity we have to mention a condition so that on true condition the Execution will stop or processor will never read goto statement. 

For clearing above points let us create an another program and understand it.

//Program to understand goto keyword with Condition in C Bhasha Gyan

#inlcude<stdio.h> 

int main()
{

int a;

ABC:                 // here Label name is ABC

printf ("Hello goto keyword in C Bhasha Gyan\n");

printf ("Enter value of a=");

scanf ("%d", &a);

if ( a ! = 1)          // if user enter the value of a is than processor will directly jump to return 0. 

goto ABC;         // at this point processor will jump on ABC Label and execute print statement again

return 0;

}

Output:
Hello goto keyword in C Bhasha Gyan
Enter value of a=2
Hello goto keyword in C Bhasha Gyan
Enter value of a=4
Hello goto keyword in C Bhasha Gyan
Enter value of a=1

I hope you all understand the conditional statement requirement with goto keyword. for practical demonstration please watch the given images-

goto Keyword in C Language
goto Keyword in C Language

Output of the Above program please watch below image-

goto keyword program output
goto keyword program output

No comments:

Post a Comment