Goto Statement in C



The goto statement in C is an equivalent of JUMP instruction in the assembly language. The goto keyword is followed by a label. When executed, the program control is redirected to the statement following the label. With goto, the program is able to jump forwards or backwards. If the label points to any of the earlier statements in a code, it constitutes a loop. On the other hand, if a label refers to any latter step, it is equivalent to a jump.

The syntax of goto statement is:

goto label;
. . .
. . .
label: statement;

The label is any valid identifier in C. A label must contain alphanumeric characters along with the underscore symbol (_). As in case of any identifier, the same label cannot be specified more than once in a program. It is always followed by a colon (:) symbol. The statement after this colon is executed when goto redirects the program here.

Flowchart

The following flowchart represents goto statement:

Goto statement in C

Example

As mentioned earlier, the goto statement can jump forward or backward. In the following program, the control jumps to a given label which is after the current statement. It prints a given number, before printing the end of program message. If it is 0, it jumps over to the printf statement displaying the message.

#include <stdio.h>

int main (){
   int n = 0;
   if (n==0)
      goto end;
   printf ("The number is: %d", n);
   end:
   printf (""end of program");
   return 0;
}

Example

Program to print if the number is even or odd:

#include <stdio.h>

int main (){

   int i = 11;
   if (i%2==0){
      EVEN:
      printf("The number is even\n");
      goto END;
   }
   else{
      ODD:
      printf("The number is odd\n");
   }
   END:
      printf("end of program");


   return 0;
}

Output

The number is odd
end of program

Example

If goto appears unconditionally and it jumps backwards, an infinite loop is created.

#include <stdio.h>

int main (){
   START:
      printf("Hello World\n");
      printf("How are you?\n");
      goto START;
   return 0;
}

The program prints the two strings continuously until forcibly stopped.

Example

The program below uses to goto statements. The second goto statement forms a loop because it makes a backward jump. The other goto jumps out of the loop when the condition is reached.

#include <stdio.h>

int main (){
   int i=0;
   START:
      i++;
      printf("i:%d\n", i);
      if (i==5)
         goto END;
         goto START;
   END:
      printf("End of loop");
   return 0;
}

Output

i:1
i:2
i:3
i:4
i:5
End of loop

Example

The goto statement is used here to skip all the value of a looping variable that matches with that of others. As a result, all the unique combinations of 1, 2 and 3 are obtained.

#include <stdio.h>

int main (){

   int i, j, k;
   for(i=1;i<=3;i++){
      for(j=1;j<=3;j++){
         if (i==j)
         goto label1;
         for (k=1; k<=3; k++){
            if (k==j || k==i)
               goto label2;
            printf("%d %d %d \n", i,j,k);
            label2: ;
         }
         label1: ;
      }
   }
   return 0;
}

Output

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Avoid goto

It may be noted that goto in C is considered unstructured, as it allows the program to jump to any location in the code, it can make code hard to understand, follow, and maintain. Too many goso statements sending the program control back and forth make the program logic difficult to understand.

Noted computer scientist Edsger Dijkstra recommended that goto be removed from all the programming languages. He observed that if the program control jumps in the middle of a loop, it may yield unpredictable behaviour. The goto statements can be used to create programs that have multiple entry and exit points, which can make it difficult to track the flow of control of the program.

Dijkstra's strong observations against the use of goto statement have been influential, as many mainstream languages do not support goto statement. However, the it still available in some languages, such as C and C++.

In general, it is best to avoid using goto statements in C. You can instead effectively use if-else statements, loops and loop controls, function and subroutine calls, and try-catch-throw statements. Use goto if and only if these alternatives don’t fulfil the needs of your algorithm.

c_loops.htm
Advertisements