Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ goto Statement

In this tutorial, we will learn about goto statement, how it works and why should it be avoided. In C++ programming with the help of some examples.


goto Statement

  • The goto statement is a jump control statement.
  • Sometimes also referred to as unconditional jump statement.
  • The goto statement can be used to from anywhere to anywhere within a function.
  • goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.

Syntax of goto Statement

goto label;
... .. ...
... .. ...
... .. ...
label: 
statement;
... .. ...

In the syntax above, label is an identifier. When goto label; is encountered, the control of program jumps to label: and executes the code below it.

Syntax1      |   Syntax2
----------------------------
goto label;  |    label:  
.            |    .
.            |    .
.            |    .
label:       |    goto label;

In the above syntax

  • The first line tells the compiler to goto or jump to the statement marked as a label.
  • The label is a user-defined identifier which indicates the target statement.
  • The goto statement immediately followed after label: is the destination statement.
  • label: can also appear before the goto label; statement.

Example 1: C++ Program to check voting eligibility using goto Statement

// This program to check if you are 18 or not using goto statement.

# include <iostream>
using namespace std;

int main(){
    noteligible:
    int age;
    cout << "You are not eligible for voting!\n";
    cout << "Enter Your age: ";
    cin >> age;
    
    if(age < 18){
        // Control of the program move to jump:
        goto noteligible;
    } 
    else{
        cout << "You are eligible for voting!";
    }
    return 0;
}

Output

You are not eligible for voting!
Enter Your age: 13
You are not eligible for voting!
Enter Your age: 26
You are eligible for voting!

Example 2: C++ Program to calculate average using goto Statement

// This program calculates the average of positive numbers entered by the user.
// If user enters negative number, it ignores and calculate only positive numbers average before it.

# include <iostream>
using namespace std;

int main()
{
    float number, average, sum = 0.0;
    int i, n;

    cout << "Number of inputs: ";
    cin >> n;

    for(i = 1; i <= n; i++)
    {
        cout << "Enter the value of n" << i << ": ";
        cin >> number;
        
        if(number < 0.0)
        {
           // Control of the program move to jump:
            goto jump;
        } 
        sum += number;
    }
    
    jump:
    average = sum / (i - 1);
    cout << "\nTotal Average = " << average;
    return 0;
}

Output

Number of inputs: 10
Enter n1: 2.3
Enter n2: 5.6
Enter n3: -5.6

Average = 3.95

You can write any C++ program without the use of goto statement and is generally considered a good idea not to use them.


Reasons why we Avoid goto Statement

  • The goto statement gives power to jump to any part of program but, makes the logic of the program complex and tangled.
  • The problem with using goto statement is that it is easy to develop program logic that is very difficult to understand, even for the original author of the code.
  • In modern programming, goto statement is considered a harmful construct and a bad programming practice.
  • It is easy to get caught in an infinite loop if the goto point is above the goto call.
  • The goto statement can be avoided in most of C++ program with the use of break and continue statements.
Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!