C Programs Tutorials | IT Developer
IT Developer

C Programming - C goto statement



Share with a Friend

C Programming - C goto statement

C goto Statement

The goto statement in C is used to transfer control to another part of the program, specifically to a labeled statement. When the goto statement is executed, the control of the program jumps directly to the location of the specified label, bypassing any intermediate code.

Syntax of goto Statement:

C

goto label;

...

label:

    // Code to be executed when control jumps to this label

  • The goto statement causes the program to jump to the label specified after the goto keyword. Labels are identifiers followed by a colon (:).
  • The control will be transferred directly to the location of the label in the program.

Example of goto Statement:

C

#include <stdio.h>

int main() {

    int i = 1;

    // Use of goto to jump to a specific label

    start:

        if (i > 5) {

            goto end;  // Jump to the end label if i is greater than 5

        }

        printf("%d ", i);

        i++;

        goto start;  // Jump to the start label

    end:

        printf("\nDone!");

    return 0;

}

Explanation:

  • The goto statement is used to jump to the start label, creating a loop-like behavior.
  • If the value of i exceeds 5, the goto statement transfers control to the end label, where the program prints "Done!" and exits.

Output:

1 2 3 4 5 Done!

Use Cases of goto Statement:

  • Looping and skipping code: You can use goto to create a loop or skip a portion of the code. However, this is not recommended because structured loops like for, while, or do-while are more readable and maintainable.
  • Error handling: In some complex programs, goto can be used for error handling, allowing the program to jump to a specific part of the code where resources can be cleaned up or errors can be processed.

Example 2: Using goto for Error Handling

C

#include <stdio.h>

int main() {

    int num = 10;

    if (num < 0) {

        goto error;  // Jump to error handling section if num is negative

    }

    printf("Number is positive.\n");

   

    // Jump to end if no error

    goto end;

error:

    printf("Error: Negative number encountered!\n");

end:

    return 0;

}

Explanation:

  • If num is negative, the control jumps to the error label, where an error message is printed.
  • If there is no error, the program continues and prints a success message, then jumps to the end label.

Output:

Number is positive.

Disadvantages of Using goto :

  1. Hard to read and maintain: Excessive use of goto makes code harder to understand and maintain, as the flow of control becomes difficult to follow.
  2. Non-structured control flow: It bypasses the structured control flow mechanisms like loops and conditional statements, which can lead to spaghetti code.
  3. Reduced modularity: Using goto makes it harder to decompose your program into well-defined functions and modules.

Best Practices Regarding goto :

  • Avoid excessive use: Use goto only when necessary, such as in specific error handling or complex conditional flows.
  • Use structured programming constructs: Where possible, prefer using loops, functions, and conditional statements instead of goto.

Summary of goto Statement:

  • The goto statement allows for an unconditional jump to a labeled statement elsewhere in the program.
  • It is generally used for error handling or when structured programming constructs are not sufficient for a specific task.
  • Avoid using goto in favor of more structured and readable control flow mechanisms like loops (for, while), functions, and conditional statements (if, switch).