C – goto Statement


Goto Statement in C



”;


What is goto Statement in C?

The goto statement is used to transfer the program”s control to a defined label within the same function. It is an unconditional jump statement that can transfer control forward or backward.

The goto keyword is followed by a label. When executed, the program control is redirected to the statement following the label.If the label points to any of the earlier statements in a code, it constitutes a loop. On the other hand, if the label refers to a further step, it is equivalent to a Jump.

goto Statement Syntax

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.

goto Statement Flowchart

The following flowchart represents how the goto statement works −


Goto statement in C

goto Statement Examples

Example 1

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 the 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;
}

Output

Run the code and check its output −


End of program

Example 2

Here is a program to check if a given number is even or odd. Observe how we used the goto statement in this program −


#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

Since the given number is 11, it will produce the following output −


The number is odd
End of program

Change the number and check the output for different numbers.

Example 3

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;
}

Output

Run the code and check its output −


Hello World
How are you?
.......
.......

The program prints the two strings continuously until forcibly stopped.

Example 4

In this program, we have two goto statements. The second goto statement forms a loop because it makes a backward jump. The other goto statement jumps out of the loop when the condition is reached.


#include <stdio.h>
int main(){
   int i = 0;
   START:
      i++;
      printf("i: %dn", i);
      if (i == 5)
         goto END;
         goto START;
   END:
      printf("End of loop");
   return 0;
}

Output

Run the code and check its output −


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

Example 5

The goto statement is used here to skip all the values 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

Run the code and check its output −


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

Avoid Using the goto Statement in C

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

Noted computer scientist dsger 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 statements. However, it is 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

”;

Leave a Reply

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