What is a goto statement in C?
The goto statement in C is a control flow statement that allows the program to jump to a labeled statement within the same function. It provides an unconditional jump from the goto to a label defined in the program.
Syntax of goto statement in C
goto label; // jump to label ... label: statement; // code to be executed after jump
- goto: This keyword tells the program to jump to a different part of the code. It sends the control to a label.
- label: A label is a name followed by a colon (:). It shows where the program should jump when it sees a goto.
Examples of Goto Statement in C
Example 1: Simple Goto Statement
#include <stdio.h> int main() { int a = 10; if (a == 10) goto jump; printf("This line will not be printed.\n"); jump: printf("Jumped to label using goto.\n"); return 0; }
Output:
Jumped to label using goto.
This program sets a to 10. Since a is 10, the program jumps to the jump label using goto. It skips the first print line and shows: “Jumped to label using goto.” This shows how goto changes the flow of the program.
Example 2: ATM Pin Retry Logic Using Goto
#include <stdio.h> int main() { int pin, attempts = 0; retry: if (attempts == 3) { printf("Card Blocked. Too many incorrect attempts.\n"); return 0; } printf("Enter 4-digit PIN: "); scanf("%d", &pin); if (pin == 1234) { printf("Access Granted.\n"); } else { printf("Incorrect PIN. Try again.\n"); attempts++; goto retry; } return 0; }
Code Output (Example Run)
Assume the user inputs incorrect PINs twice and correct PIN on the third attempt:
Enter 4-digit PIN: 1111 Incorrect PIN. Try again. Enter 4-digit PIN: 2222 Incorrect PIN. Try again. Enter 4-digit PIN: 1234 Access Granted.
Now, if the user enters an incorrect PIN three times:
Enter 4-digit PIN: 1111 Incorrect PIN. Try again. Enter 4-digit PIN: 2222 Incorrect PIN. Try again. Enter 4-digit PIN: 0000 Incorrect PIN. Try again. Card Blocked. Too many incorrect attempts.
This program asks the user to enter a 4-digit PIN. If the PIN is correct (1234), it prints “Access Granted.” If it’s wrong, it shows “Incorrect PIN. Try again.” and lets the user try again. After 3 wrong tries, it prints “Card Blocked. Too many incorrect attempts.” The goto statement sends the program back to the retry point after each wrong attempt.
Example 3: goto for Breaking Nested Loops
#include <stdio.h> int main() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i == 2 && j == 2) { goto end; } printf("i = %d, j = %d\n", i, j); } } end: printf("Exited nested loops using goto.\n"); return 0; }
Output:
i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 Exited nested loops using goto.
This program uses two loops. It prints the values of i and j. But when both i and j are 2, it uses goto to jump to the end label. This stops both loops right away. Then it prints “Exited nested loops using goto”.
Pros of Goto Statement in C
- Simplifies Error Handling: The goto statement can make error handling simpler. It lets a program jump to a common cleanup section, which is helpful when a function has many exit points. This way, you avoid repeating the same code and keep the error recovery in one place.
- Reduces Deep Nesting: It also helps reduce deep layers of if or else blocks. This can make parts of your code look more straightforward, especially when many conditions are involved.
- Useful in Finite State Machines: In some cases, like building a finite state machine, goto makes it easy to jump between states. This can save you from writing a lot of repetitive switch or if statements.
- Efficient in Low-Level Programming: In low-level or embedded programming, goto can help create more efficient machine code. That’s important in performance-heavy programs like operating systems or device drivers.
- Exits Multiple Nested Loops Easily: When working with many nested loops, goto gives you a clean way to exit to a specific point in the code. It’s often easier than using several break statements or flag variables.
Cons of Goto Statement in C
- Reduces Code Readability: Using goto can make code harder to read because it breaks the normal top-to-bottom flow. This can confuse readers, especially in large programs.
- Leads to Spaghetti Code: If used too much, goto can create what’s called “spaghetti code” — where the program jumps around so much that it becomes tangled and hard to follow. This kind of messy code is more likely to have bugs.
- Makes Debugging Difficult: Debugging and fixing code with goto is tough. When the code jumps from one label to another, it’s easy to lose track of what’s happening and how the program got there.
- Violates Structured Programming Principles: goto goes against the idea of structured programming, which focuses on using loops, conditionals, and functions to make code clear and organized. When you use goto, your code becomes less modular and harder to manage.
- Better Alternatives Are Available: In most cases, there are better options like loops, if statements, functions, or error handling tools. These make the code cleaner, easier to understand, and simpler to maintain.
Best Practices for Using goto
- Use `goto` only when necessary, like for breaking out of nested loops or centralizing error handling.
- Avoid using it to jump backward as it can create infinite loops.
- Always document the purpose of the `goto` and the label it jumps to.
- Try to replace `goto` with functions, loops, or flags wherever possible for better structure.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice Computer Science MCQs
- Check C Books
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check Computer Science Books