C++ goto Statement
In this tutorial, we will learn about goto statement, how it works and why should it be avoided. In C++ programming with the help of some examples.
goto Statement
- The
goto
statement is a jump control statement. - Sometimes also referred to as unconditional jump statement.
- The goto statement can be used to from anywhere to anywhere within a function.
- goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.
Syntax of goto Statement
goto label; ... .. ... ... .. ... ... .. ... label: statement; ... .. ...
In the syntax above, label is an identifier. When
goto label;
is encountered, the control of program jumps to
label:
and executes the code below it.
Syntax1 | Syntax2 ---------------------------- goto label; | label: . | . . | . . | . label: | goto label;
In the above syntax
- The first line tells the compiler to goto or jump to the statement marked as a label.
- The label is a user-defined identifier which indicates the target statement.
- The goto statement immediately followed after label: is the destination statement.
- label: can also appear before the
goto label;
statement.
Example 1: C++ Program to check voting eligibility using goto Statement
// This program to check if you are 18 or not using goto statement.
# include <iostream>
using namespace std;
int main(){
noteligible:
int age;
cout << "You are not eligible for voting!\n";
cout << "Enter Your age: ";
cin >> age;
if(age < 18){
// Control of the program move to jump:
goto noteligible;
}
else{
cout << "You are eligible for voting!";
}
return 0;
}
Output
You are not eligible for voting! Enter Your age: 13 You are not eligible for voting! Enter Your age: 26 You are eligible for voting!
Example 2: C++ Program to calculate average using goto Statement
// This program calculates the average of positive numbers entered by the user.
// If user enters negative number, it ignores and calculate only positive numbers average before it.
# include <iostream>
using namespace std;
int main()
{
float number, average, sum = 0.0;
int i, n;
cout << "Number of inputs: ";
cin >> n;
for(i = 1; i <= n; i++)
{
cout << "Enter the value of n" << i << ": ";
cin >> number;
if(number < 0.0)
{
// Control of the program move to jump:
goto jump;
}
sum += number;
}
jump:
average = sum / (i - 1);
cout << "\nTotal Average = " << average;
return 0;
}
Output
Number of inputs: 10 Enter n1: 2.3 Enter n2: 5.6 Enter n3: -5.6 Average = 3.95
You can write any C++ program without the use of goto
statement and is generally considered a good idea not to use them.
Reasons why we Avoid goto Statement
- The goto statement gives power to jump to any part of program but, makes the logic of the program complex and tangled.
- The problem with using goto statement is that it is easy to develop program logic that is very difficult to understand, even for the original author of the code.
- In modern programming, goto statement is considered a harmful construct and a bad programming practice.
- It is easy to get caught in an infinite loop if the goto point is above the goto call.
- The goto statement can be avoided in most of C++ program with the use of break and continue statements.
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of goto Statement in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Functions
.