×

goto statement in C and C++

goto statement in C and C++

The goto statement is a jump statement, also sometimes referred to as an unconditional jump statement. Within a function, the goto statement can be used to jump from anywhere in general conditions.

NOTE ? the use of goto statements in any programming language is strongly discouraged because it makes it difficult to track a program's control flow, making it very difficult to understand the program and difficult to modify. 

Syntax:

Syntax1      |   Syntax2
----------------------------
goto label_name;
..
..
..
label_name: C-statements

The first line in the syntax above tells the compiler to go, or jump to, the statement marked as a label. The label here is a user-defined identifier indicating the target declaration. The statement followed immediately after 'label:' is the state of destination. In the above syntax, the 'label:' may also appear before the 'goto label;' statement.

goto statement in C and C++

Example:

1. We shall see a similar situation in this case to the one shown above in Syntax1. Assume we have to write a program where we have to check whether there is a number even or not and use the goto statement to print consequently.The program below explains how:

// C++ program to check if a number is
// even or not using goto statement
#include <iostream>
using namespace std;
// function to check even or not
void checkEvenOrNot(int num)
{
    if (num % 2 == 0)
    // jump to even
        goto even; 
    else
    // jump to odd
        goto odd; 
even:
    cout << num << " is even";
    // return if even
    return; 
odd:
    cout << num << " is odd";
}
// Driver program to test above function
int main()
{
    int num = 35;
    checkEvenOrNot(num);
    return 0;
}

Output:

goto statement in C and C++

Example in C:-

// C program to check if a number is
// even or not using goto statement
#include <stdio.h>
// function to check even or not
void checkEvenOrNot(int num)
{
    if (num % 2 == 0)
        // jump to even
        goto even; 
    else
        // jump to odd
        goto odd; 
even:
    printf("%d is even", num);
    // return if even
    return; 
odd:
    printf("%d is odd", num);
}
int main() {
    int num = 35;
    checkEvenOrNot(num);
    return 0;
}

Output:

goto statement in C and C++

2. In this case, we can see a similar situation to the one seen in Syntax1 above. Suppose we have to write a program using the goto statement, which prints numbers from 1 to 10. The program below explains how to do so.

// C++ program to print numbers
// from 1 to 10 using goto statement
#include <iostream>
using namespace std;
// function to print numbers from 1 to 10
void printNumbers()
{
    int s = 5;
label:
    cout << s << " ";
    s++;
    if (s <= 20)
        goto label;
}
// Driver program to test above function
int main()
{
    printNumbers();
    return 0;
}

Output:

goto statement in C and C++

Example in C:

// C program to print numbers
// from 5 to 20 using goto statement
#include <stdio.h>
// function to print numbers from 5 to 20
void printNumbers()
{
    int s = 5;
label:
    printf("%d ",s);
    s++;
    if (s <= 20)
        goto label;
}
// Driver program to test above function
int main() {
    printNumbers();
    return 0;
}

Output:

goto statement in C and C++

Disadvantages of using goto Statement:

The use of the goto statement is highly disincentive as it makes the logic of the program very complex.

The use of goto makes it very difficult to evaluate and verify the correctness of the programs (especially those that require loops).

Using break and continue statements can simply avoid the use of goto.


Related Topics

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

C++ Memory Management

Memory management is a method of controlling computer memory and allocating memory space to applications to increase overall system performance. What is the purpose of memory management? Because the array contains homogeneous...

4 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

Dynamic Constructor in C++

Dynamic constructors create dynamic memory by using a dynamic memory allocator new within the constructor. This allows us to initialize objects dynamically. The term dynamic constructor is used when memory...

2 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

Call by Pointer in C++

What is Pointer? Every variable in C++ has a specific address or location in the computer's memory, and this address is known as the memory address. A pointer can be defined...

5 minutes read.

Functions in C++ with Types and Examples

A function is a collection of statements that work together to complete a certain goal. It could consist of statements that execute repetitive operations or statements that conduct specialized jobs...

10 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

How to Setup Environment for C++ Programming on Mac

Mac OS X code Installation There are so many environments available for C++. We are going to install jGrasp and Xcode in our mac operating system. Instruction for installation of jGrasp and...

2 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

Implementing the sets without C++ STL containers

Many practical features and tools in C++ support us in programming competitions. One of these parts is a set from the Standard Template Library (STL), which offers an effective way...

6 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

3 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.