C++ goto Statement

The goto statement in C++ is also referred to as the jump statement. It is utilized to provide the other portion of the program control. It always jumps to the designated label.

It can be used to transfer control from a switch case label or a deeply nested loop.

NOTE: The usage of goto statements is strongly discouraged since they make it difficult to trace a program’s control flow, which makes it difficult to comprehend and alter. Any program that employs a goto can be revised to do without it.

#include <iostream>
using namespace std;
// geektocode.com
int main()
{
ineligible:
 cout << "You are not eligible!\n";
    cout << "Enter your age:\n";
    int age;
    cin >> age;
    if (age < 18)
    {
        goto ineligible;
    }
    else
    {
        cout << "You are eligible to vote!";
    }
}
OUTPUT:
You are not eligible!
Enter your age:      
5
You are not eligible!
Enter your age:      
55
You are eligible to vote!

Leave a Reply

Your email address will not be published. Required fields are marked *