Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
How to Write Getter and Setter Methods in C++?
Next article icon

How to Utilize the goto Statement in C++?

Last Updated : 28 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The goto statement in C++ is a control flow statement that allows the users to move the control flow from one part to another part of the program. In this article, we will learn how to utilize the goto statement in C++.

Utilize the goto Statement in C++

In C++, the goto statement transfers the program's control flow from one part to another. It allows the users to jump to another part of the program while skipping the current part. Although it is generally advised to use structured control flow statements for readability and maintainability, there are many scenarios where the goto statement can be useful. Following is the syntax for goto:

Syntax

#include <iostream>

int main() {
// ... code before goto

goto label; // Jump to the label

// ... code that will be skipped by the goto

label:
// ... code after the label

return 0;
}

C++ Program to Utilize the goto Statement

The following program demonstrates how we can utilize the goto statement to break out of multiple nested loops

C++
// C++ Program to Utilize the goto Statement #include <iostream> using namespace std;  int main() {     for (int i = 0; i < 3; ++i) {         for (int j = 0; j < 3; ++j) {             if (i == 1 && j == 2) {                 // Exit both loops                 goto endLoops;               }             cout << i << ", " << j << endl;         }     }  endLoops:     cout << "Exited the nested loops." << endl;      return 0; } 

Output
0, 0  0, 1  0, 2  1, 0  1, 1  Exited the nested loops.  

Time Complexity: O(1), as goto is used to terminate the loop iterations
Auxiliary Space: O(1)

Utilize goto for error handling

The following program illustrates how goto statement can be used for error handling in order to avoid unnecessary calculations:

C++
// C++ Program to utilize goto for error handling #include <iostream> #include <limits> using namespace std;  int main() {     double numerator = 10.0;     double denominator = 0.0;      double result;      if (denominator == 0) {         // Jump to cleanup if denominator is 0         goto cleanup;     }      // Perform the division     result = numerator / denominator;     cout << "Result: " << result << endl;  cleanup:     cerr << "Error: Division by zero is not allowed." << endl;     cout << "Program has finished executing." << endl;      return 0; } 


Output

ERROR!
Error: Division by zero is not allowed.
Program has finished executing.

Time Complexity: O(1)
Auxiliary Space: O(1)


Next Article
How to Write Getter and Setter Methods in C++?

K

kumardhanshiv
Improve
Article Tags :
  • C++ Programs
  • C++
  • C-Loops & Control Structure
  • CPP-Control-Flow
  • CPP Examples
  • misc-cpp
Practice Tags :
  • CPP
  • CPP-Control-Flow

Similar Reads

  • How to Read Input Until EOF in C++?
    In C++, EOF stands for End Of File, and reading till EOF (end of file) means reading input until it reaches the end i.e. end of file. In this article, we will discuss how to read the input till the EOF in C++. Read File Till EOF in C++The getline() function can be used to read a line in C++. We can
    2 min read
  • How to Use the ignore() Function in C++?
    In C++, the ignore() function is a part of std::basic_istream that is used to discard the characters in the stream until the given delimiter(including it) is reached and then extracts the left-out remainder. In this article, we will learn how to use the ignore() function in C++. C++ ignore() Functio
    2 min read
  • How to iterate over the elements of an std::tuple in C++
    A C++ tuple is a container that can store multiple values of multiple types in it. We can access the elements of the tuple using std::get(), but std::get() always takes a constant variable parameter, so we can not simply iterate through it using a loop. For tasks that require iterating through all e
    6 min read
  • How to Use the Not-Equal (!=) Operator in C++?
    The not-equal operator is a fundamental comparison operator in C++ represented by "!=". It is used for making decisions in programming and is hence called a conditional operator. In this article, we will discuss how to use the Not-Equal (!=) operator in C++. Not-Equal (!=) Operator in C++The not-equ
    3 min read
  • How to Write Getter and Setter Methods in C++?
    In C++, classes provide a way to define custom data types that can encapsulate data and methods related to that data. Getter and setter methods are commonly used to access and modify private member variables of the class allowing for the controlled access and modification of data. In this article, w
    2 min read
  • How to Use cin.fail() Method in C++?
    In C++, the cin.fail() method is a part of <iostream> library that is used to check whether the previous input operation has succeeded or not by validating the user input. In this article, we will learn how to use cin.fail() method in C++. Example: Input: Enter an integer: aOutput: Invalid Inp
    2 min read
  • How to Take Multiple Input from User in C++?
    In C++, we use cin when we want to take input from the user. We often also need to take more than one input at a time. In this article, we will learn how to take multiple inputs in C++. Take Multiple Inputs from a User in C++To take multiple inputs from users, we can repeatedly use the std::cin usin
    2 min read
  • How to Find the Type of an Object in C++?
    In C++, every variable and object has a type. The type of an object determines the set of values it can have and what operations can be performed on it. It’s often useful to be able to determine the type of an object at runtime, especially when dealing with complex codebases. In this article, we wil
    2 min read
  • How to Convert Float to int in C++?
    In C++, the float is a short term for floating point value that is used to define numeric values with decimal points. The floating point numbers can be converted to integers using the process called type casting. In this article, we will learn how to convert a float value to an int in C++. Example:
    2 min read
  • How to Add Multiple Conditions for if Statement in C++?
    In C++, an if statement is used to decide whether a specific block of code should execute, depending on a given condition. It usually evaluate a single expression to check condition. In this article, we will learn how to add multiple conditions as a single expression for an if statement in C++. Mult
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences