Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Goto and Return Statements in C++

Goto and Return Statements in C++

21 May 2024
Beginner
2.05K Views
7 min read
Learn with an interactive course and practical hands-on labs

Free C++ Course: Learn C++ In 21 Days

Goto and Return Statements in C++: An Overview

In the previous C++ tutorial on jump statements, we saw the break and continue statements in C++ in complete detail. It's now time to move on to the other two important jump statements, goto and return statements in C++. If you want to become a certified C++ programmer, you can also consider our C++ Certification Course.

Goto Statement in C++

It is a control flow mechanism that enables programmers to move the execution of a program to a particular labeled statement inside the code. The goto statement in C++ allows one to skip over the customary sequential flow of the code and go to a specific location within it. The best practice is to avoid the requirement for goto statements whenever possible by using structured programming elements like loops and conditionals.

Goto Statement in C++

Syntax

goto label;
// ...
label:
// Statement(s) to execute

Example to demonstrate goto statement in C++ Compiler

#include <iostream>
using namespace std;
int main() {
 int choice;
 cout << "Select an option:" << endl;
 cout << "1. Print 'Hello'" << endl;
 cout << "2. Print 'World'" << endl;
 cout << "3. Exit" << endl;
 cin >> choice;

 switch (choice) {
 case 1:
 cout << "Hello" << endl;
 break;
 case 2:
 cout << "World" << endl;
 break;
 case 3:
 cout << "Exiting..." << endl;
 goto end; // Jump to the 'end' label to exit
 default:
 cout << "Invalid choice" << endl;
 break;
 }

 // This is where the 'end' label is defined
 end:
 cout << "Program ends here." << endl;
 return 0;
}
  • This C++ program gives the user a menu to select an option.
  • Depending on what they select, the program either prints "Hello," "World," or ends.
  • When the user chooses the exit option 3, the goto statement is used to leap to the end label, enabling the program to end and output "Programme ends here."

Output

Select an option:
1. Print 'Hello'
2. Print 'World'
3. Exit

  • If the user enters 1st choice

    Output

    Hello
    Program ends here.
    
  • If the user enters 2nd choice

    Output

    World
    Program ends here.
    
  • If the user enters 3rd choice

    Output

    Exiting...
    Program ends here.
    

Read More - C++ Interview Questions Interview Questions for Freshers

Return Statement in C++

When a function is finished running, the return statement in C++ is used to give the caller a value. It is often used to return a result to the caller code.

Return Statement in C++

Syntax

return expression;

Example to demonstrate return statement in C++

 #include <iostream>
 using namespace std;
 int multiply(int a, int b) {
 return a * b;
 }
 
 int main() {
 int result = multiply(5, 7);
 cout << "The product is: " << result << endl;
 return 0;
 } 
  • Here, the main() function calls the multiply() function to multiply the values 5 & 7.
  • The multiply() function multiplies the values and returns the product to the main() function.

Output

The product is: 35

Discover more about functions and use of the return statement in the section, Functions in C++, Call by Value and Call by Reference in C++.

Summary
With this, we have now completed all four jump statements in C++. You will now see the use of these statements in our further tutorials on C++. As we have already seen these statements in much detail, you won't have a problem understanding their usage. For a more in-depth understanding, consider our C++ Certification Course.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this