C++ goto Statement

C++ goto Statement

The goto statement in C++ is a control flow statement that allows unconditional jumps to a labeled part of a program in the same function. Although the goto statement is a part of the C++ programming language, it is still generally advised not to use it in modern C++ programming. In this article, we will discuss what a goto statement is, its syntax, properties, examples, disadvantages, when to use it, reasons to avoid it, and preferred alternatives to the goto statement in C++.

Table of Contents:

What is a goto statement in C++?

A goto statement in C++ is a control flow statement that helps programs to jump or switch to another part of the same function. It provides an unconditional jump within the same function. Also, when a goto statement is executed then the control transfers directly to the specified label with skipping code in between. Structured alternatives such as loops, conditional statements, and exception handling are preferred instead of using a goto statement.

Syntax of a goto statement in C++

Below is a general syntax of a goto statement in C++:

goto label;
// ... some code ...
label:
// code to execute after jump

Here, the label is a user-defined identifier with a colon(:), which shows the target location in the code.

Example:

Cpp

Output:

goto statement in C++ Example

The code shows how the goto skip; statement helps the program to jump directly to the skip: label by skipping the line “This will not be printed”, and then the execution resumes from the label and prints “After goto”.

Flowchart of a goto Statement in C++

Flowchart of a goto Statement in C++

Properties of the C++ goto statement

  • It transfers control to a labeled statement without any condition.
  • Both the goto and the target label must be in the same function.
  • A label is an identifier with a colon(:).
  • The code between the goto and its label is skipped during execution.

Examples of goto Statement in C++

Below are a few examples of goto statements in C++:

Example 1: Input Validation Using goto

Cpp

Output:

Example 1: Input Validation Using goto

The code shows how a goto statement is used to repeatedly to give the messages to the user until a valid age input between 0 and 120 is entered, and then the result is printed to the console.

Example 2: Infinite Loop Using goto

Cpp

Output:

Example 2: Infinite Loop Using goto

The code shows how an infinite loop is created using goto, which repeatedly jumps back to the loop_start label, and then prints the message “Welcome to Intellipaat” infinite times.

Example 3: Breaking Out of Nested Loops

Cpp

Output:

Example 3: Breaking Out of Nested Loops

The code shows how a goto is used to exit from both nested loops when i == 2 && j == 2, and then resumes execution at the exit_loops label to print a message and output.

Example 4: Conditional Branching using goto

Cpp

Output:

Example 4: Conditional Branching using goto

The code shows how a goto statement is used to jump to either to even or odd label based on the given input by the user, and then prints the result to the console.

Example 5: Simple Menu Using goto

Cpp

Output:

Example 5: Simple Menu Using goto

The code shows how the goto statement is used to display a menu and to handle the user input until the user chooses the exit option, and then print the result to the console.

When to Use goto Statement in C++

Below are a few conditions when a goto statement is used in C++:

  • Breaking out of deeply nested loops, after encountering a situation that requires jumping out of multiple nested loops or conditionals at once, in this situation, a goto statement can make the process easier.
  • Error handling in low-level C++ code.
  • When there is a need to jump to clean a section of the code before returning the value.

Reasons to Avoid goto Statement in C++

Here we are going to discuss a few disadvantages of using goto statement in C++

  • A goto statement makes the code more difficult to understand.
  • It does not follow the modern programming principles, which provide clear, modular, and flow control practices.
  • It skips the constructor and destructor, which leads to memory leaks and resource management problems.
  • It should be avoided because better alternatives, such as loops, conditionals, exceptions, and functions, make code reusable, safer, and readable.

Preferred Alternatives to goto Statement in C++

Use Case Preferred Alternative Why These Are Better Than goto
Looping for, while, do-while These loops are clean, clear, and easier to manage.
Exiting nested loops break, return It gives you a clean exit without jumping all over the cases.
Skipping code blocks continue, if-else Keeps the code flow simple and predictable.
Error handling try-catch, RAII Safer, and ensures resources are cleaned up the right way.
Repeating input logic Loop with condition No need to manually jump back, just use a proper loop.
Cleaning before return RAII, smart pointers They handle cleanup for you automatically, goto is not needed.

Conclusion

The goto statement in C++ helps in switching to a labeled part of code in the same function. It has some practical applications, but it is mainly advised not to use the goto statement for a few reasons. Also, there are various disadvantages of the goto statement, due to which it is better to use other alternatives such as loops and conditional statements. So, by understanding what a goto statement is, when to use it, and when to avoid it, you can easily write a C++ program using the goto statement.

C++ goto Statement – FAQs

Q1. Is goto recommended in modern C++?

No, it is not recommended in modern C++ except in some cases, such as breaking out of nested loops and handling low-level errors.

Q2. Can a goto statement jump between two functions?

No, a goto statement can only jump to a part within the same function.

Q3. What are alternatives to goto?

The alternatives to the goto statement are loops, break, continue, return, and exceptions.

Q4. Does goto affect destructors?

Yes, a goto statement can skip destructors and break RAII if it is not used properly.

Q5. Is goto faster than loops or conditionals?

No, the goto statement is not faster than loops or conditionals, but there is also a negligible performance difference in some cases.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner