goto Statement in C

In this article, we will study about goto Statement in C. We will learn about the syntax of goto Statement in C and the working of goto Statement in C. Moreover, we will learn about various advantages and disadvantages of goto Statement in C.

The goto statement in C is an unconditional jump statement. The Goto Statement in C is used to jump from one part of the code to any other part of the code in C.

This goto Statement transfer the control of the flow of the program to a predefined label. The goto statement can be used in two different ways:

  • To repeat some lines of code by going above in the code
  • To skip some lines and move to a block below.

What is goto Statement in C?

Consider that you have enrolled in an advanced course in a subject after finishing a beginner’s course in that field. For students who are brand-new to the subject, the first few courses of this course review the fundamental concepts. However, since you have already studied these, you can skip over them and move straight to the lectures that begin the advanced session.

Think about a second instance where you enrolled in a different advanced course for a different subject. However, this time you didn’t enroll at the course’s basic level, and there aren’t any revision lectures, so your tutor told you to first familiarise yourself with the course’s foundational material. In this instance, you initially access the novice course before returning to complete the advanced course.

Similarly, when we need to jump from one block to another block to another, we use goto statement in C. It is often also known as an unconditional jump statement.

Syntax of goto Statement in C

The syntax of goto Statement in C consists of two main parts:
1. Defining label
labelName:

  • The labelName acts as an identifier for the block.
  • It must end with a colon (:).
  • Each label name must be unique
  • It cannot be a reserved keyword.

2. Transfer of Execution control
goto labelName;

  • When the goto statement in C is encountered, the flow of the program get transferred to the block labeled as labelName.
  • This statement ends with a semicolon.

The complete syntax of goto Statement in C can be written as:

labelName:
.
.
.
goto labelName;

We can use use goto statement before the declaration of the labelName as given below:

 goto labelName;
 .
 .
 .
 labelName:

So, the well-defined syntax for goto statement in C is:

#include 

int main()
{
    statement1;
    ...

    labelName:
        statement2;
        statement3;
        ...
        if(condition)
            goto labelName;

    return 0;
}

In the code given above, if the condition is true, the flow of the program will get transferred to the labelName.

Flow Diagram of goto Statement in C

In the above-shown flowchart, we have defined 3 labels as Label1, Label2 and Label3.
As soon as the compiler reaches Statement 1, the flow of the program will shift to Label3 because the use of goto Statement in C and Label2 will not execute.

Examples of How to use goto Statement in C

1. Absolute Value of a Specified Integer using goto statement
The absolute of a number is defined as the magnitude of a given integer. It is a non-negative value of an Integer. For example,

  • |87| = 87
  • |-98| = 98

#include <stdio.h>
#include <math.h>

int main()
{
    int n = -1001;
    
    // if the number is already positive, print it directly
    if(n>=0){
        // goto statement
        goto positive;
    }
    
    n = n*(-1);
    
    // declare positive label
    positive :
        printf("The absolute value is %d", n);
    
    return 0;
        
}

Output:

The absolute value is 1001

Explanation: The given program check whether the number is positive or negative. If the number is positive, the flow is directly transferred to the label “positive”. If the number is negative, it goes through another condition n=(-1)*n; which converts it into a positive value.

2. Checking whether the number is odd or even by using goto statement in C
The number divisible by 2 is even, if not divisible it is called an odd number.

#include 

int main() {
    int num = 26;
    if (num % 2 == 0)
        // jump to even
        goto even;
    else
        // jump to odd
        goto odd;

    even:
        printf("%d is even", num);
        // return if even
        return;
    odd:
        printf("%d is odd", num);
    return 0;
}

Output:

26 is even

Explanation: In this first, we are checking if the number is giving the remainder 0 or not when divided by 2. If the remainder is 0, then the flow of the program is transferred to the label “even”, and if the remainder is 1, then the flow of the program is transferred to the label named “odd”.

When we should use goto Statement in C

  1. The condition in which goto statement is preferred to use is when we need to break multiple loops only with the help of one statement.
    The following code has explained this:
#include <stdio.h>  
int main() {  
  int i, j, k;    
  for(i=0;i<5;i++) {  
    for(j=0;j<5;j++)  {  
      for(k=0;k<3;k++) {  
        printf("%d %d %d\n",i,j,k);  
        if(j==2) {  
          goto out;   
        }  
      }  
    }  
  }  
  out:   
  printf("came out of the loop");   
}

Output:

0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
came out of the loop

Explanation: The program is running three nested loops for printing variables. But as soon as the loop reaches the condition “j==2”, it jumps directly to the label “out”.

  1. goto statement in C helps us to initiate loops in a program without using for or while in code.

Disadvantages of using goto Statement in C

Although goto helps us in transferring flow from one block to another block, the use of goto statement in C is discouraged because of the following reasons:

  • It makes the logic of the program much more complex.
  • The use of goto Statement in C can make the code much harder to follow.
  • It makes the code unreadable and thus tricky to debug.
  • Use of two or more labels in goto Statement in C generally leads to loss of structure of code.

An example of the buggy and hard-to-follow code because of the use of goto statement in C is

one: 
for(int i=0; i6){
    goto three;
}
….. …
..

In the above sample program, use of goto statement is transferring control from label “one” to label “two”, and then from label “two” to label “three”. This makes code difficult to follow and thus makes it difficult to debug the code.

Alternatives of goto Statement in C

The use of goto statement in C can be avoided by using break and continue statements.

  • break: break is a loop control statement that is used to terminate loops.
  • continue: continue statement is a loop control statement that forces the program control to execute the next iteration of the loop.

Summary
The goto statement in C is an unconditional jump statement that is used to jump from one part of the code to any other part of the code in C.

  • The syntax of goto Statement in C consists of two main parts:

    • Defining Label
    • Transfer of Execution Control
  • The complete syntax of goto Statement in C can be written as:

    labelName:
    .
    .
    .
    goto labelName;
  • The only condition in which goto statement is preferred to use is when we need to break multiple loops only with the help of one statement.

  • The use of goto statement in C is discouraged since it makes the code more difficult to understand, trickier to debug, and leads to loss of structure of code.

Leave a Reply

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