goto STATEMENT IN C/C++
What is goto statement in C/C++?
The goto statement is a
jump statement in C/C++ which is also called unconditional jump statement. It
is used to transfer control from any point in a function to some other point (called
label).
Let us look into the working of goto
statement. Here we define a label (which is a user-defined identifier).
We then write a statement or a block of statements under that label. So
now whenever we refer to that label, we are actually referring to those
statements. Whenever the compiler reads the statement goto label, it
passes the control to that label and starts executing from there. Note
here that a label name can be any valid identifier.
Here are 2 basic syntax for goto
statement :
Type 1
: label : statement;
…//may be some code
goto label;
Type 2
: goto label;
…//may be some code
label : statement;
What is the scope of goto statement?
A goto statement is valid only if it
passes control to a label in the same function. Referencing a label outside the
function in which it is defined gives compilation error. But within a function
a goto statement and a label can be placed anywhere (i.e. maybe type 1 or type
2 ).
See the following code :
void
Sum()
{
label
: cout<< “Sum”;
}
int
main()
{
goto
label;
return
0;
}
This code gives the following error
message:
[error] label ‘label’ used
but not defined
Let us see a demo code and understand
its working using a flowchart.
//demo run for goto statement
label1
: statement1;
goto
label3;
label2
: statement2;
label3
: statement3;
//find and print all odd numbers
//from 1 to 10 using goto
#include<iostream>
using
namespace std;
int
main()
{
int n= 1;
label :
if(n%2 != 0)
cout<<n<< “ ”;
n++;
if(n<=10)
goto label;
return 0;
}
Output : 1 3 5 7 9
Executing both if & else statement simultaneously
We know, in using if-else construct,
either the if or the else statement is executed but never both. This is
required for correct decision making process. However, the statement that if
and else statements cannot be executed simultaneously is not true.
As we have learnt earlier in this article,
goto statement can be used to transfer control from any point to the other in a
function. So, can we use it to jump from if part to else part? The answer is
yes! Let us see an example :
//code to jump from if to else
if(condition)
{
statement1;
goto label2;
}
else
label2 : statement2;
Now if the condition is true, the
control will enter if and execute statement1. Then it will jump to label2 and
execute statement2.
However always be careful when using
goto because you may easily end up having an infinite loop.
Why goto statement is not used by programmers?
You must have noticed that although the
concept of goto sounds so important, it is not generally used in programs.
There are a few reasons for it :
First, using goto in a program makes
the logic very complex. You need to take extra care about the control positions
and be sure the control doesn’t get trapped in an infinite loop.
Second, reading a program developed by
some other programmer who has used goto can be really tough. It becomes very
hard to debug and correct the program.
Third, better and safer options of jump
control are available in C/C++ as break and continue statements. These
statements give the desired result without causing much trouble.
If you want to add something or correct
some information in this post, comment down on this post or contact coding.nkcoder@gmail.com.
Which topics you want an article on? Comment down on this
post.
Do not stop here. Read all articles related to C++ here.
If you want to contribute any article for the website, feel
free to share it on coding.nkcoder@gmail.com. We will
publish it on the website with your name.
Download the pdf form of this article here.
Post a Comment