The goto
statement is a type of unconditional transfer statement, similar to the goto
statement in BASIC.
In C programming, the goto
statement is used to transfer control unconditionally from one point in a program to another. However, the use of goto
is generally discouraged because it can make the code less readable and harder to understand. It can lead to “spaghetti code,” where the flow of control is difficult to follow.
Here’s the syntax of the goto
statement in C:
goto label; ... // ... // label: statements
label
is an identifier followed by a colon. It is a marker that indicates the destination point for thegoto
statement.- The
goto
statement transfers control to the labeled statement.
Example 1) Output 0 to 4 using goto statement
#include <stdio.h> int main() { int i = 0; loop_start: if (i < 5) { printf("%d ", i); i++; goto loop_start; // Jump back to the label } return 0; }
In this example, the program prints numbers from 0 to 4 using a goto
statement to create a loop. However, it’s important to note that using loops (for
, while
, do-while
) is generally a better practice than using goto
for controlling program flow.
Output of the above program:
0 1 2 3 4
As mentioned earlier, using goto
can make the code less readable and harder to maintain. In modern programming, structured control flow constructs like loops and conditionals are preferred over goto
for better code organization and maintainability.
Example 2) Calculating the sum of numbers from 1 to 100
main() { int i,sum=0; i=1; loop: if(i<=100) { sum=sum+i; i++; goto loop; } printf("%d\n",sum); }
Output:
Example 3) Calculating the sum and average of numbers entered by the user by goto statement
// Program to calculate the sum and average of positive numbers // If the user enters a negative number, the sum and average are displayed. #include <stdio.h> int main() { const int maxInput = 100; int i; double number, average, sum = 0.0; for (i = 1; i <= maxInput; ++i) { printf("%d. Enter a number: ", i); scanf("%lf", &number); // go to jump if the user enters a negative number if (number < 0.0) { goto jump; } sum += number; } jump: average = sum / (i - 1); printf("Sum = %.2f\n", sum); printf("Average = %.2f", average); return 0; }
Output:
1. Enter a number: 3 2. Enter a number: 4.3 3. Enter a number: 9.3 4. Enter a number: -2.9 Sum = 16.60 Average = 5.53
Should You Use goto Statement ?
The use of the goto
statement is generally discouraged in modern programming practices. While it can be a powerful tool for control flow, it often leads to less readable and maintainable code. Here are some reasons why the goto
statement is not recommended:
- Readability and Maintainability: Code that uses
goto
can be more challenging to read and understand. This can make it difficult for others (or even yourself) to maintain and modify the code in the future. - Structured Programming: The concept of structured programming encourages the use of structured control flow constructs like loops and conditionals (e.g.,
for
,while
,if-else
) to make the code more organized and easier to follow. The use ofgoto
can disrupt this structure. - Debugging: Code with
goto
statements can be harder to debug because the flow of control can jump to different parts of the code unexpectedly. This makes it more difficult to trace and understand the program’s execution. - Scope Issues:
goto
can cause variable scope issues, as variables might be declared in one part of the code and used in a different part after agoto
jump. - Structured Alternatives: In most cases, there are structured alternatives to using
goto
. For example, loops and functions can be used to achieve similar control flow without sacrificing code structure.
That being said, there may be some specific scenarios where the use of goto
can be justified, such as breaking out of nested loops or handling errors in a cleanup routine. However, in general, it’s advisable to use structured programming constructs to make your code more readable, maintainable, and less error-prone.