C

C – Example Of A Goto Statement

The goto statement is rarely used because it causes program confusion, makes it less readable, and makes it more complex. Furthermore, when this is used, the program’s control will be difficult to trace, making testing and debugging difficult.

C – statement of goto

When a goto statement is encountered in a C program, control is immediately transferred to the label specified in the goto statement.
The syntax of the goto statement in C

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

A goto statement is an example

#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 addition, and when the value of I (inside loop) equals 5, we use goto to jump to this label. This is why, despite the fact that the loop is set to run from 0 to 10, the sum displays the sum of numbers up to 5.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button