Table of Contents
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:
goto
statement followed by the label’s identifier. For example:goto
statement is executed.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
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
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.goto
can make it challenging to trace the flow of the program, which is crucial for debugging and understanding how the code works.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
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
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
Ans. C program consists of three types of Jump Statements in C, namely, break, continue, and goto.
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