In this lesson, you will learn how to code the goto statement in the C programming language. You will also learn when to utilize it and when.
The goto Statement, which is used to unconditionally branch within a program from one point to another, is a special type of statement that C supports. The goto statement can be useful in some circumstances, despite the fact that it is not recommended to use it frequently in C.
By moving control to a new section of the same program, programmers can modify the order in which a C program is executed by using the goto statement.
The goto statement is a jump statement, often known as an unconditional jump statement in some instances. Within a function, you can hop from one place to another by using the goto statement.
In C, the goto statement is referred to as the jump statement. Goto is used to moving program control to a predefined label, as the name implies.
To repeat a section of code for a certain circumstance, use the goto statement.
Additionally, it can be used to end several loops that can’t be broken with a single break statement. Today, however, most avoid using goto since it makes programs more complex and difficult to read.
We can move program control to the designated label using the goto statement.
Contents
Goto Statement Syntax:
goto label; ... .. ... ... .. ... label: statement;
A label serves as identification. The control of the program moves to label: and begins running the code when the goto instruction is encountered.
An identification needed for a goto statement to the location where the branch is to be made is called a label.
A label is a legal variable name that goes before the statement where control has to be unconditionally jumped or transferred, followed by a colon.
The first line of the syntax above instructs the compiler to navigate to or jump to the states designated as a label.
Here, the target statement is identified by the user-defined identifier label. The destination statement is the one that comes just after “label:”. In the syntax above, the label: can also come before the phrase “goto label;”.
Example: 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
Enter a number: 3
Enter a number: 4.3
Enter a number: 9.3
Enter a number: -2.9
Sum = 16.60
Average = 5.53
Reasons to avoid goto
The use of goto statements could result in illegible and error-prone code. For instance,
one: for (i = 0; i < number; ++i) { test += i; goto two; } two: if (test > 5) { goto three; } ... .. ...
Additionally, the goto statement gives you the option to do undesirable things like exit the scope.
Having stated that, goto occasionally has a use. To break out of nested loops, for instance.
Do you want to utilize goto?
You can use the goto statement if you believe it makes your program simpler. In spite of this, goto is rarely useful, and you may write any C program without it.
Bjarne Stroustrup, the inventor of C++, is quoted as saying, “The fact that ‘goto’ can accomplish anything is exactly why we don’t use it.”
You can use our compiler to practice what you studied