goto Statement in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Goto statement in C is a jump statement that is used to jump from one part of the code to any other part of the code in C. Goto statement helps in altering the normal flow of the program according to our needs. This is achieved by using labels, which means defining a block of code with a name, so that we can use the goto statement to jump to that label. Goto statement can be used in two ways: either to skip some lines and move to a block below in the code or to repeat some lines of code by going above in the code.

goto statement in C

Consider you have signed up for an advanced course for a subject after you completed it's beginner course. The first few lessons of this course revise the beginner topics for students who are new to the subject. But you have already studied these, so you will skip these beginner lectures, and directly goto the lectures where the advanced lesson starts.

Consider another case where you signed up for a different advanced course for another subject. But this time you didn't take its beginner course and there are no revision lectures hence your tutor instructed you to first get familiar with the beginner course. In this case you first goto the beginner course and then come back to finish this advanced course.

Similarly, we may need to jump from one line to another in our program to either goto skip a few lines or to first execute some block of code and then arrive at this one. This is exactly where the goto statement is used.

Goto statement is a form of jump statement, it is used to jump from one block to another block during execution. It is often also termed as an unconditional jump statement.

Syntax of goto Statement in C

The syntax of goto statement in C can be broken into two parts:

1. Defining the Label

label_name:

  • The label_name is used to give a name to a block of code, hence it acts as an identifier for that block. When a goto statement is encountered, the program's execution control goes to the label_name: and specifies that the code will be executed from there.
  • We need to always use : (colon) after the label_name
  • Each label_name has to be unique in the scope where it has been defined and cannot be a reserved word, just like in variables.

2. Transferring the Execution Control

goto label_name;

  • The above statement jumps the execution control of the program to the line where label_name is used.

Now by combining the above two parts, we get the full syntax of the goto statement in C. However there's a catch, We can combine the above two parts in two different ways.

Style 1: Transferring the Control From Down to the Top

Style 2: Transferring the control from top to down

Before we move on to discussing these two methods in detail, let’s take a look at the general flow diagram of the goto statement in C.

Flowchart of goto Statement in C

Flow Diagram of goto Statement in C

As visible in the flow diagram, as soon as we arrive at the goto statement, the control of the code is transferred to wherever the label has been defined.

In this case, the label has been defined below the goto statement. Hence, the statements between the goto statement and the label declaration are skipped.

On the other hand, the label can also be declared before the goto statement. In this case, no statement is skipped, instead, the statements between the goto statement and the label declaration are repeated.

This has been shown in the image below.

Declaring Lable for goto Statement in C

Some Examples of how to use a goto statement

There are two different styles of implementing goto statements in C. Either the label is declared above the call of the goto statement, which is the first case, or the label is declared after the call of the goto statement.

In the first style, the flow control shifts from the lower to some upper part of the code, while in the second style the flow control shifts from the upper part to the lower part of the code, maybe skipping some lines in between.

Let us look at both of these styles in detail to understand more.

Style 1: Transferring the Control From Down to the Top

In this style, the control is transferred to a part of the program which is above the goto statement. This results in a kind of loop in the program. We will see this more clearly in the example.

Lets take a look at a modified and well defined syntax again:

In the above pseudo-code, when and if the condition is true the program execution control will be transferred to label_name. Let’s take an example where we might use such logic.

Example 1: To print numbers using the goto statement

Output

It is clear from this program that whenever curr is less than the end, we repeat the printing part until curr becomes equal to the end. At that point, the condition inside if becomes false, and the goto statement isn't executed.

This is exactly how a goto statement can create a loop in a program, without using for or while loops. However, in most cases goto statements are used only for flow control, to dictate where the control of the program should be transferred next.

Style 2: Transferring the Control From Top to Down

This style has the same syntax, with the only exception being that the label is declared after the goto statement is called. This means that in this case, the control is transferred to a part of the program which is below the goto statement.

Let us take a took at the syntax:

In the above pseudo-code, if the condition is true, the control is passed on to the label block. Let's see an example.

Example 2: To Find Ceil Division of Two Numbers

Output

In this program, we add one to the answer only if a is not divisible by b, else the program goes straight to the print_line label because of the goto statement. Thus, in this way, the goto statement helps in transferring the control in the code.

When should We Use the goto Statement?

In any case, where you need to make a jump from one part of the code to another, you can use the goto statement. It can be used to move to any place within the function where the goto statement is called, depending upon where the label referred to by the goto statement has been declared.

It is important to note that using goto statements we can even jump to a part of the code in our program which we have already passed and executed once if the label has been defined above the goto statement. This was the first style discussed above. This is unlike many other jump statements like a break, which transfer the code execution to a part somewhere below the current execution.

Disadvantages of using goto Statement in C

Goto statements, although help in jumping from one part of the code to another, make code unreadable. Just imagine having more than two or three goto statements in a program, and trying to figure out which goto statement leads the code to which part. As goto statements alter the normal flow of execution of the program, it can become difficult to understand the new flow when goto statements are added. This makes code unreadable and dirty.

Let us take an example to understand it better.

Output

This code looks straight enough and runs fine, but only for odd numbers. If you look closely, this code goes into an infinite loop for even numbers because of line 24. This little cycle is being formed due to the goto statement in this line, and it is difficult to figure it out because of the changing flow of execution of the code due to the goto statements.

This is just a small example of how goto statements can cause unwanted loops and errors in programs if not used with caution.

Conclusion

  • The goto statement in C is used to jump from one block to another block during execution, and transfer the flow of execution of the code.
  • The syntax of the goto statement can be divided into two parts:
    1. Defining the label.
    2. Transferring the execution control.
  • There are two different styles of implementing the goto statement in C:
    1. Transferring the control from down to the top.
    2. Transferring the control from top to down.
  • Goto statements can be used in any case where you need to make a jump from one part of the code to another if-else and that it is useful in altering the normal flow of execution of code.
  • Although useful, goto statements can make the code unreadable and difficult to maintain and debug, and hence should be used carefully.