in , ,

C goto Statement

C goto Statement
C goto Statement

C goto Statement

In this tutorial, you will learn to make the goto statement in C programming. Also, you will realize when to use a goto statement and when not to use it.

The goto statement allows us to move control of the program to the predefined label.

Syntax of goto Statement

goto label;
... .. ...
... .. ...

The label is an identifier. When the goto statement is encountered, the control of the program jumps to label: and starts executing the code.

goto label

Example: goto Statement

// Program to calculate the sum and average of positive numbers
// If the user enters a negative number, the sum and average are displayed.

#include <stdio.h>

int main() {

   const int maxInput = 100;
   int i;
   double number, average, sum = 0.0;

   for (i = 1; i <= maxInput; ++i) {
      printf("%d. Enter a number: ", i);
      scanf("%lf", &number);
      // go to jump if the user enters a negative number
      if (number < 0.0) {
         goto jump;
      sum += number;

   average = sum / (i - 1);
   printf("Sum = %.2f\n", sum);
   printf("Average = %.2f", average);

   return 0;


1. Enter a number: 3
2. Enter a number: 4.3
3. Enter a number: 9.3
4. Enter a number: -2.9
Sum = 16.60
Average = 5.53

Reasons to avoid goto

The use of a goto statement may lead to code that is buggy and hard to follow. For example,

for (i = 0; i < number; ++i)
    test += i;
    goto two;
if (test > 5) {
  goto three;
... .. ...

Also, the goto statement allows you to do awful stuff, for example, leap out of the degree.

That being stated, goto can be valuable at times. For instance: to part from nested loops.

Should you use goto?

If you think the use of the goto statement improves your program, you can utilize it. That being stated, goto is once in a while valuable and you can make any C program without using goto altogether.

Here’s a quote from Bjarne Stroustrup, creator of C++, “The fact that ‘goto’ can do anything is exactly why we don’t use it.”

Please feel free to give your comment if you face any difficulty here.

salman khan

Written by worldofitech

Leave a Reply

Internet Safety for Kids

Internet Safety for Kids

WordPress SEO

WordPress SEO Tutorial (From Beginner To Advanced Guide) – 2021