Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

goto Statement in C

Last Updated on April 13, 2023 by Prepbytes

The goto statement in C is a controversial feature that has been a topic of discussion among programmers for decades. The goto statement can be useful in certain cases, but it can also lead to unreadable and unmaintainable code if used improperly. In this article, we will discuss the basics of the goto statement in C programming language. Before learning about the goto statement in C, it is recommended to first have a basic understanding of control structures in C.

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
Here’s an explanation of the label part in goto statement in C.

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
Here’s an explanation of how the transfer of control happens.

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.

FAQs

Here are some frequently asked questions on the goto statement in C.

Q1: What are the benefits of using a goto statement in C programming?
Ans: The goto statement in C can be used to simplify code and reduce nesting levels in certain cases, as well as to implement error handling.

Q2: Can the goto statement in C be used to jump to a label defined in another function?
Ans: No, the goto statement in C can only jump to a label defined within the same function.

Q3: Can the goto statement in C be used in switch statements or loops?
Ans: Yes, the goto statement in C can be used in switch statements or loops, but it is generally not recommended as it can make the program logic difficult to follow.

Q4: Can the goto statement be used for jumping to a statement that is defined before the goto statement?
Ans: No, the goto statement can only jump to a statement that is defined after the goto statement.

Q5: What is the difference between the break and goto statements in C programming?
Ans: The break statement is used to exit a loop or switch statement, while the goto statement in C is used to transfer control to a labeled statement within the same function.

Q6: Can the goto statement in C be used for implementing exception handling?
Ans: Yes, the goto statement in C can be used for implementing exception handling, but it is generally not recommended.

Q7: Is the use of the goto statement in C considered bad practice?
Ans: Not necessarily, the use of the goto statement in C can be justified in certain cases, but it is generally considered a bad practice as it can make the program logic difficult to follow and understand.

Leave a Reply

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