Explain GoTo Statement in C ?

Explain GoTo Statement in C ?

The GOTO statement is rarely used because it makes the program confusing, less readable, and complex. Also, when it is used, it will not be easy to locate the program’s controls, so it makes testing and debugging difficult.

When a goto statement is encountered in a C program, control goes directly to the label specified in the goto statement.

Read Also – C – break statement in C programming

Syntax of goto statement in C :

goto label_name;
..
..
label_name: C-statements

Flow Diagram of goto statement

Explain GoTo Statement in C

Example of goto statement in c

#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: 

In this example, we have a label add and when the value of i is equal to 5 we are going to this label using goto. This is why sum is displaying the sum of numbers up to 5, even though the loop is set to run from 0 to 10.

Tag –

Explain GoTo Statement in C ?

Explain GoTo Statement in C

Mahesh Wabale

Leave a Comment