goto Statement


The C Goto statement is a control flow statement that allows a program to jump to a different part of the code based on a label.
It allows the programmer to transfer the programโ€™s control to a specific labeled statement within the same function. However, the general consensus discourages its usage due to its potential to make code hard to understand and maintain.

Syntax

The basic syntax of the goto statement in C is as follows:

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

ย Hereโ€™s, how the goto Statement works:

  • goto: This is the keyword that indicates you want to use the goto statement.

  • label: This is the name of the target statement where you want to jump. Within the same function, the label must be unique and followed by a colon (:).

For instance:

#include <stdio.h>
int main() {
    int number;

    // This is the label for the goto statement.
    input_number:
    printf("Enter a number between 1 and 100: ");
    scanf("%d", &number);

    if (number < 1 || number > 100) {
        printf("Invalid input.\n");
        goto input_number; 
   // If input is invalid, jump back to the "input_number" label.
    }

    printf("You entered: %d\n", number);
    return 0;
}

Hereโ€™s how it the for loop works:

  1. The program starts by declaring a variable number to store the userโ€™s input.

  2. The program then encounters the input_number label, which is just a marker in the code.

  3. It prompts the user to enter a number between 1 and 100 using printf and reads the input using scanf.

  4. Next, it checks if the entered number is less than 1 or greater than 100 using the if condition.

  5. If the input is not within the desired range (1 to 100), it prints an error message indicating that the input is invalid.

  6. Now, the interesting part occurs: when the program executes the goto statement, it jumps back to the label โ€œinput_number.โ€ This means it will re-display the prompt and ask the user to enter the number again.

  7. The program keeps repeating the steps from 3 to 6 until the user provides a valid number within the specified range.

  8. If the user enters a number within the valid range, the if condition evaluates to false, and the program proceeds to print the message โ€œYou entered: [number]โ€ indicating the successful input.

  9. Finally, the program returns 0, indicating successful execution.

The โ€œgotoโ€ statement in this code creates a loop-like behavior, prompting continuous user input until valid input is provided.

However, modern programming discourages the use of goto in this manner because it can make code more difficult to read and understand. Instead, structured loops like while or do-while are usually preferred for such repetitive tasks.


Reasons to avoid goto statement

The goto statement is a contentious feature in programming languages like C, allowing control transfer to labeled statements within the same function. Though it can achieve certain tasks, it is widely considered harmful and discouraged due to various reasons:

  1. Readability and Maintainability: Code that uses goto can become hard to read and understand, making it challenging for other programmers (or even yourself) to follow the flow of the program. This lack of readability can lead to bugs and make maintenance difficult.

  2. Unstructured Code: goto can lead to unstructured code and make it more challenging to reason about the programโ€™s behavior. Structured control flow statements like if, while, and for provide a clear and organized way to control the programโ€™s flow.

  3. Spaghetti Code: Excessive use of goto statements can result in โ€œspaghetti code,โ€ where the programโ€™s flow becomes tangled and difficult to trace.

  4. Debugging Difficulties: When bugs occur in code that uses goto, it can be harder to trace the issue back to its source, as the control flow is non-linear.

  5. Error-Prone: Misusing goto can lead to logical errors, unintended behavior, and unexpected results in the program.

  6. Poor Software Engineering Practice: Most modern software engineering practices discourage the use of goto. The use of structured control flow is considered more elegant and maintainable.

In rare situations, people may consider goto acceptable, such as when breaking out of nested loops or handling resource cleanup in error scenarios. However, better alternatives usually exist, like using break or employing functions for resource cleanup.

In summary,ย generally avoid using the goto statement, opting for structured control flow constructs that enhance code readability and maintainability. This approach leads to cleaner, easier-to-understand code, reducing bugs and facilitating collaboration.