Goto Statement in C
Hello Everyone, In this article, we will cover the concept of Goto Statement in C Programming. In this topic we will cover what is goto statement, its syntax, how it works, and practical examples of its use. Whether you’re new to C or have some experience, this guide will help you to understand Goto Statement in C effectively. Let’s get started.
In C programming, the “Goto Statement” is a control statement that allows the program to jump to a different section of code based on a specified label.
Syntax of the goto Statement
goto label;
Here, label is a user-defined identifier that marks a specific location within the program’s code.
For example:
#include <stdio.h>
int main() {
int i = 0;
start:
printf("%d ", i);
i++;
if (i < 10) {
goto start;
}
return 0;
}
In this example, the start label marks the beginning of a loop that repeatedly prints the value of i until it reaches 10. The goto start; statement causes the program to jump back to the start label and repeat the loop until the condition is no longer true.
While the “goto” statement can be useful in certain situations, it is generally considered a bad practice to use it because it can make the code difficult to read and understand.
The output of the above program would be:
0 1 2 3 4 5 6 7 8 9
This is because the program defines a loop that starts at 0 and prints the value of i on each iteration, incrementing i by 1 until it reaches 10. The goto statement is used to jump back to the beginning of the loop, allowing the loop to execute 10 times in total.