Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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,
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 1 | Syntax 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.
Let’s see few examples on how to use goto statement in C++
//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:
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:”.
//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:
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.
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.
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 28th September,2024 28th September SAT&SUN (Weekend Batch) | View Details |
edureka.co