Introduction: In this article i am going to explain what is goto statement or we can say labelled statement, what are its advantages and disadvantages and a sample program to calculate sum of two numbers and prompting whether to continue with more calculations or not using goto statement in C++ Language.
Description: In previous C++ related articles i explained the program How to find greatest of two numbers using conditional operator in C++ Language and How to find greatest of two numbers using if else statement and Else if ladder to perform operation on two numbers based on operator and How to get number of days in month,year using nested if.
In this article and program i will explain what goto statement is and how to use it. Goto statement is used to alter the normal sequence of program execution by transferring the control of execution to some other part of the program unconditionally. So the control of flow transfers unconditionally to the statement specified by the label of goto statement.
Syntax for goto:
Where goto is a keyword and label can be any valid identifier.
In above syntax it is clear that after the execution of goto statement the control will jump unconditionally from there to statement-n which is prefixed by the label, without executing in between statements.
using goto statement you can alter the normal sequence of the program execution so it gives the power to jump to any part of program.
It is always recommended not to use goto statement as this reduces the readability of the program. It becomes difficult to trace the control flow of a program, making the program logic complex to understand .Using goto statement is considered as poor programming practice. Any program written in C language can be written without the use of goto statement. So try to avoid goto statement as possible as you can.
There are two types of jump in goto statement:
1. Forward jump
2. Backward jump
In the example program below both the forward jump and backward jump is used.
Implementation: Let's create the program to understand.
y:cout<<"Enter any two numbers= ";
cout<<"First Number= "<<a<<endl;
cout<<"Second Number= "<<b<<endl;
cout<<"Sum of " <<a<< " and "<<b<<" = "<<a+b<<endl;
cout<<"Do you want more calculations ? Enter y for Yes and n for No= ";
cout<<"Invalid choice...Enter y for Yes and n for No";
n:cout<<"Please press enter to exit...HAVE A NICE DAY AHEAD!!!";
Enter any two numbers= 40
First number= 40
Second number= 60
Sum of 40 and 60 = 100
Do you want more calculations? Enter y for Yes and n for No= y
Enter any two numbers= 12
First number= 12
Sum of 12 and 13 = 25
Do you want more calculations? Enter y for Yes and n for No= n
Please press enter to exit...HAVE A NICE DAY AHEAD!!!");
Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."