Open In App

Goto Statement in Programming

Last Updated : 22 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Goto statement is a control flow statement present in certain programming languages like C and C++. It enables programmers to transfer program control to a labeled section of code within the same function or block. Despite its availability, its usage is often discouraged in modern programming practices due to its potential to complicate code structure and reduce readability. In this article, we will discuss about a Goto statement used in programming.

What is a Goto Statement?

The goto statement, found in languages like C and C++, allows the program to jump to a labeled section of code within the same function or block. The goto statement can be used to jump from anywhere to anywhere within a function. 

Syntax of Goto Statement:

The basic syntax of the goto statement is as follows:

goto label;

Here, the label is a predefined identifier followed by a colon (:), marking a specific location in the code.

Common Use Cases of Goto Statement:

While the goto statement can be misused and lead to “messy code,” there are some scenarios where it may be considered useful:

  • Breaking out of nested loops.
  • Jumping to a common error-handling section.
  • Implementing state machines or complex state transitions.

However, its usage is often discouraged due to its potential to create complex and difficult-to-follow code structures, making the code harder to understand and maintain.

Goto Statement in C:

Here are the implementation of goto statement in C language:

C
#include <stdio.h>

int main() {
    int i = 0;

    loop_start:
    if (i < 5) {
        printf("%d ", i);
        i++;
        goto loop_start;
    }

    return 0;
}

Output
0 1 2 3 4 

Goto Statement in C++:

Here are the implementation of goto statement in C++ language:

C++
#include <iostream>
using namespace std;
int main() {
    int i = 0;

    loop_start:
    if (i < 5) {
        cout << i << " ";
        i++;
        goto loop_start;
    }
    return 0;
}

Output
0 1 2 3 4 

Goto Statement in C#:

Here are the implementation of goto statement in C# language:

C#
using System;

class Program {
    static void Main() {
        int i = 0;

        loop_start:
        if (i < 5) {
            Console.Write(i + " ");
            i++;
            goto loop_start;
        }
    }
}

Output
0 1 2 3 4 

Goto Statement is not supported in Java, Python and JavaScript.

Best Practices of Goto Statement:

  • Avoid Usage: In general, it is recommended to avoid using goto as it can lead to difficult-to-understand and error-prone code.
  • Structured Programming: Utilize structured programming constructs like loops and conditionals to achieve the desired flow without resorting to goto.
  • Exception Handling: For error-handling scenarios, prefer using exception handling mechanisms provided by the language.
  • Code Review: If goto is deemed necessary, ensure thorough code reviews to mitigate potential issues and maintain code quality.

Conclusion:

In Conclusion, the goto statement, present in languages like C and C++, allows programmers to jump to a labeled section of code. However, its use is discouraged due to its tendency to make code less readable and harder to maintain. Many modern programming languages do not include the goto statement as part of their syntax.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads