C goto Statement with Example

Go to statement in c programming language; In this tutorial, i am going to show you what is go to statement in c programming language and 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

Let’s take first example of using 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

Illustration of above given c program; is as follows:

  • 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

Be First to Comment

Leave a Reply

Your email address will not be published.