Java/J2EE and SOA (349 Blogs) Become a Certified Professional

How To Implement Goto Statement In C++?

Last updated on Aug 26,2019 43K Views

How To Implement Goto Statement In C++?

No matter what the programming language, programmers  have hard time when it comes to traversing between the code. In this article we would be exploring ‘Goto Statement In C++’ that helps us simplify the process of traversing along the code.

Following are pointers to be discussed in this article,

So let us start with article by understanding the first topic,

What is Goto Statement in C++?

The goto statement in C++ is an unconditional jump statement used for transferring the control of a program. It allows the program’s execution flow to jump to a specified location within the function. There are two ways to call the goto statement.

Syntax 1Syntax 2

goto label;

//block of statements

label:  ;

label:  ;

//block of statements

goto  label;

A label’s name is a user defined identifier and is distinguished by the colon that immediately follows its name. The statement immediately followed after “label:” is the statement to be executed after goto statement. The goto statement jump to the statement marked with a label.

Examples Of Goto Statement

Let’s see few examples on how to use goto statement in C++

Example 1:


//based on syntax 1
#include<iostream>
using namespace std;
// function to check greater number
void checkGreater()
{
int i, j;
i=2;j=5;
if(i>j)
goto iGreater;
else
goto jGreater;
iGreater:
cout<<i<<" is greater";
return;
jGreater:
cout<<j<<" is greater";
}
// main method to test above function
int main()
{
checkGreater();
return 0;
}

Output:

output - Goto Statement in C++ - Edureka

The return statement after “iGreater:” in “checkGreater” function. Once the control jumps to the label with “iGreater:”, the program will execute every piece of code after it. So it is important to return if the number is greater. Otherwise the code after label “jGreater:” will also gets executed as it comes after “iGreater:”.

Example 2:

 
//based on Syntax 2
#include <iostream> 
using namespace std; 
// function to print numbers from 1 to 5
void printNumbers() 
{ 
int n = 1; 
print: 
cout << n << " "; 
n++; 
if (n <= 5) 
goto print; 
} 
// main method to test above function 
int main() 
{ 
printNumbers(); 
return 0; 
}

Output:

output - Goto Statement in C++ - Edureka

In the above program, the label is named as “print” and the goto statement jumps to the “print” label only when the variable “n” is less than or equal to 5.

Why not to use Goto Statement?

Early programming languages like FORTRAN and early versions of BASIC did not have structured statements like while, so programmers were forced to use goto statements to write loops. The problem with using goto statements is that it is easy to develop program logic that is very difficult to understand, even for the original author of the code.

It is easy to get caught in an infinite loop if the goto point is above the goto call.

How to avoid goto statement?

Goto isn’t inevitable and can be avoided. Goto statement can be avoided using break and continue statements.

This brings us to the end of this article on ‘Goto Statement In C++’.  I hope you enjoyed this piece of information. Now that you have understood the above mentioned concept, if you are interested in similar content or training check out these courses by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Edureka’s training and certification course are designed for students and professionals who want to excel in their professions. The course is designed to give you a head start into your preferred and train you for the respective certification or professional goals you wish to achieve in the respective domain of interest.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 28th September,2024

28th September

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Implement Goto Statement In C++?

edureka.co