Goto Statement In C https://www.skillvertex.com/blog Fri, 10 May 2024 07:20:31 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Goto Statement In C https://www.skillvertex.com/blog 32 32 Goto Statement In C https://www.skillvertex.com/blog/goto-statement-in-c/ https://www.skillvertex.com/blog/goto-statement-in-c/#respond Fri, 10 May 2024 07:20:31 +0000 https://www.skillvertex.com/blog/?p=2318 Read more]]>

Table of Contents

Goto Statement In C

The goto statement in the C programming language. The goto statement is indeed a jump statement that allows for unconditional transfers of control within a function. It allows you to transfer the program’s control to a specific labeled location within the same function or code block.

Syntax

Syntax1 | Syntax2

—————————-

goto label; | label:

. | .

. | .

. | .

label: | goto label;

The goto statement is used to transfer control to a labeled statement within the same function or code block. The label is indeed a user-defined identifier, and the statement immediately following the label is the destination statement.

You define a label using an identifier followed by a colon (:). For example:

  1. You place this label before the statement you want to mark as the destination.
  2. To jump to the labeled statement, you use the goto statement followed by the label’s identifier. For example:
  3. The control flow will transfer to the statement marked by the label when the goto statement is executed.

Example: Type 1

Type 1: Here’s an example of a C program that uses the goto statement to check if a number is even or not and then prints the result accordingly:


// C program to check if a number is 
// even or not using goto statement 
#include <stdio.h> 
  
// function to check even or not 
void checkEvenOrNot(int num) 
{ 
    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); 
} 
  
int main() 
{ 
    int num = 26; 
    checkEvenOrNot(num); 
    return 0; 
}

Output

26 is even

Example: Type 2

Type 2: Here’s an example of a C program that uses the goto statement to print numbers from 1 to 10

// C program to print numbers 
// from 1 to 10 using goto statement 
#include <stdio.h> 
  
// function to print numbers from 1 to 10 
void printNumbers() 
{ 
    int n = 1; 
label: 
    printf("%d ", n); 
    n++; 
    if (n <= 10) 
        goto label; 
} 
  
// Driver program to test above function 
int main() 
{ 
    printNumbers(); 
    return 0; 
}

Output

1 2 3 4 5 6 7 8 9 10

Disadvantages of Using Goto Statement

  1. Complexity: The goto statement can make program logic complex and hard to follow. It can create “spaghetti code” where the flow of control is not evident, making it difficult for programmers to understand and maintain the code.
  2. Difficulty in Tracing: Using goto can make it challenging to trace the flow of the program, which is crucial for debugging and understanding how the code works.
  3. Verification Difficulty: Programs that use, especially in loops, can be challenging to analyze and verify for correctness. It becomes harder to reason about the program’s behavior and ensure it behaves as intended.
  4. Alternatives: In many cases, structured control flow constructs like loops (for, while, do-while) and conditional statements (if, else) provide more readable and maintainable alternatives to achieve the same tasks. Additionally, break and continue statements are often used to control loops without resorting to goto

FAQ- Goto Statement In C

Q1. What is a goto statement in C?

Ans. the goto statement in C. It is indeed a jump statement that allows you to transfer control from one part of the code to any other part of the code within the same function or code block. The goto statement provides the flexibility to alter the normal flow of a program according to specific needs or conditions

Q2. What is a goto statement in C with an example?

Ans. The use of a goto statement may lead to code that is buggy and hard to follow. For example, one: for (i = 0; i < number; ++i) { test += i; goto two; } two: if (test > 5) { goto three; } … .. … Also, the goto statement allows you to do bad stuff such as jump out of the scope

Q3. Is goto a jump statement in C?

Ans. C program consists of three types of Jump Statements in C, namely, break, continue, and goto.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/goto-statement-in-c/feed/ 0