Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

goto Statement in C – Explained with Examples

Basics of Python
Basics of Python
icon
5 Hrs. duration
icon
9 Modules
icon
1800+ Learners
logo
Start Learning

The goto statement in C language is used to jump from one part of the source code to another. It can be very helpful in certain situations where other control statements might need to be more effective. Goto statements can be used in two ways: skip some lines and move to a block below in the source code or repeat some lines of source code by going above in the source code.  This article also explained the ‘goto’ statement in C language. Its disadvantages of goto

goto Statement in C

The ‘goto’ statement in C is a control flow statement that allows you to jump to another point in the program. It is generally recommended to use structured control flow statements like loops and conditionals, ‘goto’  can be useful in certain situations, particularly when handling errors or breaking out of deeply nested loops.

Syntax of goto Statement in C

The syntax of the goto statement can be defined in two parts:

Defining the Label

  • The label_name is defined as the block of code. When a goto statement is encountered, the program execution goes to the label_name and runs the label code.
  • We need to always use a colon after the label_name
  • Each label_must be unique in its defined scope and cannot be a reserved word, just like any variables.

Transferring the Execution Control

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

 

Style 1: Down to the top declaration of the goto statement in C

label_name: . . . goto label_name;

Style 2: Transferring the control from top to down

label_name: . . . goto label_name;

Flowchart of goto Statement in C

Flowchart of goto Statement in C

As shown in the flow diagram, when the program encounters a goto statement, the control immediately jumps to the location where the corresponding label is defined.

 

In the scenario where the label is defined after the goto statement, the program skips over any statements between the goto statement and the label.

 

Conversely, no statements are skipped if the label is declared before the goto statement. Instead, the statements between the goto statement and the label are executed again, effectively repeating them.

 

Some Examples of How to Use a goto Statement

Let’s look at examples of ‘goto’ statements in the C language:

go to statement in c++

Example 1: Basic Use of ‘goto’

#include <stdio.h> int main() { int x = 10; if (x == 10) { goto label1;  // Jump to label1 } printf("This line will be skipped if x is 10.n"); Label1: printf("This line is executed because x is 10.n"); return 0; }

Output

This line is executed because x is 10.

The above program checks whether the condition is equal to 10. If it is, the goto label1 statement transfers control to the label1 function and directly executes the code after the label.

 

Example 2:  The following program demonstrates the ‘goto’ for Error Handling

#include <stdio.h> int main() { int a = 10, b = 0; int result; if (b == 0) { goto error;  // If b is zero, jump to the error handling code } result = a / b; printf("Result: %dn", result); return 0; error: printf("Error: Division by zero!n"); return -1; }

Output

ERROR! Error: Division by zero!
DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

Advantages of Using goto Statement in C Language

The ‘goto’  statement in C is often criticised, but it does have some advantages in certain scenarios. Let’s see some potential benefits of using ‘goto’:

 

  • Simplicity in Error Handling: The complex functions with multiple nested loops and conditionals, ‘goto’ can simplify error handling by allowing you to jump to a common cleanup.

 

  • Breaking Out of Nested Loops: When you need to exit from multiple levels of nested loops or switch statements, ‘goto’ can provide a straightforward solution. It can be more intuitive than using multiple ‘break’ statements or flags to control loop execution.

 

  • Performance: Modern compilers optimise source code well. The ‘goto’ can sometimes lead to more efficient source code by avoiding the overhead of additional control statements. It is particularly relevant in performance-critical sections of source code.

 

  • State Machines: These are the low-level programming tasks, such as implementing state machines. The ‘goto’ can create a straightforward and efficient state transition mechanism.

Disadvantages of Using goto Statement in C Language

The ‘goto’ statement in C language allows an unconditional jump to a labelled statement within the same function. Imagine having more than two or three goto statements in a program and figuring out which one is leading the program. It can become difficult to understand the new flow when goto statements are added. This makes the source unreadable and dirty.

 

The following program demonstrates the show goto Statement in C language:

 

Program

#include <stdio.h> int main(){ int n = 5; int ans = n; start : ans += n; if(ans%2==1){ goto odd; } if(ans%2==0){ goto start; } odd : printf("%d", ans); return 0; }

Output

15

Conclusion

The ‘goto’ statement in C allows an unconditional jump to a labelled statement. It can simplify certain tasks but often results in less readable and maintainable source code. Modern programming practices generally prefer structured control flow mechanisms like loops and conditionals over ‘goto’ to avoid potential pitfalls such as increased complexity and difficulty in debugging. It is used ‘goto’ sparingly and only when it improves source code clarity or simplifies complex logic.

FAQs
The ‘goto’ statement in C language provides an unconditional jump from the ‘goto’ statement to a labelled statement within the same function. It transfers control directly to the labelled source code. It skips any intermediate source code.
C does not have built-in exception handling, but ‘goto’ can be used for error handling and cleanup tasks. This approach can mimic some aspects of exception handling by jumping to an error-handling section of source code.
Modern programming languages provide alternatives to ‘goto’ such as structured exception handling. It is a very comprehensive control flow construct and a higher-level abstraction that avoids the need for an unconditional jump.
Yes, ‘goto’ can handle multiple error conditions by jumping to a common error-handling label. This can simplify managing errors in functions with complex logic and multiple potential failure points.
‘goto’ does not directly interact with function pointers. However, using ‘goto’ jump to the source that involves function pointers. However, using ‘goto’ to jump to source code that involves function pointers should be done cautiously to avoid confusing control flow and potential errors.

Deploying Applications Over the Cloud Using Jenkins

Prashant Kumar Dey

Prashant Kumar Dey

Associate Program Director - Hero Vired

Ex BMW | Google

19 October, 12:00 PM (IST)

Limited Seats Left

Book a Free Live Class

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.

Data Science

Accelerator Program in Business Analytics & Data Science

Integrated Program in Data Science, AI and ML

Accelerator Program in AI and Machine Learning

Advanced Certification Program in Data Science & Analytics

Technology

Certificate Program in Full Stack Development with Specialization for Web and Mobile

Certificate Program in DevOps and Cloud Engineering

Certificate Program in Application Development

Certificate Program in Cybersecurity Essentials & Risk Assessment

Finance

Integrated Program in Finance and Financial Technologies

Certificate Program in Financial Analysis, Valuation and Risk Management

Management

Certificate Program in Strategic Management and Business Essentials

Executive Program in Product Management

Certificate Program in Product Management

Certificate Program in Technology-enabled Sales

Future Tech

Certificate Program in Gaming & Esports

Certificate Program in Extended Reality (VR+AR)

Professional Diploma in UX Design

Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved