Go to statement in c programming; Through this tutorial, you will learn how to use go to statement in c program.
C Programming goto Statement
When a goto statement is encountered in a C program, the control jumps directly to the label mentioned in the goto statement
Syntax of goto Statement in c programming
See the following syntax of go to statement in c; as shown below:
goto label; ... .. ... ... .. ... label: statement;
The label is an identifier. When the goto
statement is encountered, the control of the program jumps to label:
and starts executing the code.
Example 1 – Goto Statement in C program
See the following example for goto statement in c; as shown below:
#include <stdio.h> int main() { int sum=0; for(int i = 0; i<=10; i++){ sum = sum+i; if(i==5){ goto addition; } } addition: printf("%d", sum); return 0; }
Output
15
Explanation of above c program:
- Iterate for loop and specify condition inside the loop is i == 5
- Then jumping to this label using goto
- This is reason the sum is displaying the sum of numbers till 5 even though the loop is set to run from 0 to 10