goto statement in C Language
goto statement in C programming language is used for unconditional jump from one part of the program to another part of the program. It is always suggested not to use goto statement as this reduces the readability of the program. Using goto statement in C programming language is considered as poor programming approach.
The goto
statement consists of two parts: label and goto keyword.
Example:
/*use of goto statement*/ #include<stdio.h.> #include<conio.h> void main(){ int a; goto label; a = 10; printf(“%d”, a); label: a = 20; printf(“%d”, a); }
Output:
20
When goto
label is encountered, control goes to the statement next to the label. Here, label is not the keyword. We can use any name for the label. It is user defined.
what is the alternative approach then?
can we use conditions with goto ?
like goto label if (a=10)
?
You should be able to use..
if (a == 10){
goto label;
}
respected sir or madam would like to give another example using goto statement,to understand its usage clearly and properly.please.please.please.please reply at your earliest possible.
#include
#include
void main()
{
int i;
label1:
printf(“\n\n\t Enter the value of i:- “);
scanf(“%d”,&i);
if(i>0)
{
printf(“\n\n\t Thank you for positive value…,”);
goto label2;
}
else
{
printf(“\n\n\t Only Positive Numbers Pls..,”);
goto label1;
}
label2:
printf(“\n\n\t World didn’t end here…,”);
getch();
}
what does these statements mean?
#include
#include
The #include preprocessor directive is used to paste code of given file into current file. It is used include system-defined and user-defined header files. If included file is not found, compiler renders error. By the use of #include directive, we provide information to the preprocessor where to look for the header files.
#include are preprocessors
means they contain predefined functions..which are later used in program:)
please give another example for using goto in more complex way..like loops in loops
#include are the preprocessors that will tell the compiler to include the text in the file STDIO.H before it executes its source code.
Thanks readers for your valuable comments.
In addition, We have recently started a new forum.
http://forum.learnconline.com/
You can post your queries and get it resolved.
Thanks,
LearnCOnline.com
Learning becomes easy here..thank you
ThaNk yew sooo mucH. This site iz very Helpful 4 us to leaRn moRe about C:)
great site
Sir, how to decide which text file should be include in pre-processor directive ?