Goto statement in C [ Full Information With Examples ]
In C language, the goto statement is known as the jump statement because the goto statement is used to transfer the control of the program to an already defined label.
The goto statement can also be used to replicate certain parts of the program to certain conditions.
It can also be used to come out of multiple loop statements at once which cannot be done using a break statement.
Despite these uses of goto statement, using the goto statement in any programming language is avoided because it changes the flow of the program making it harder to understand and later modify the program.
Syntax of goto Statement:
You can use goto statement in two ways which Syntax is something like this:
Syntax 1 -:
goto label_name;
..
..
label_name:
Syntax 2 -:
label_name:
.
.
.
goto label_name;
Let’s now create a program using both methods.
Examples of goto statement
In this example, we will first create a program of syntax type1.
Type 1 -: Here we will create a program with the help of goto statement in which we will check a number and tell whether the number is Odd number or an even number.
/ * C program to check if a number is
even or not using goto statement
* /
#include <stdio.h>
int main ()
{
int num = 26;
if (num%2 == 0)
goto even; // jump to even
else
goto odd; // jump to odd
even:
printf ("%d is even", num);
// return if even
return 0;
odd:
printf ("%d is odd", num);
return 0;
}
Output -:
26 is even
Example2: In this example, we will create a program of another syntax type2.
Type 2-:
Here we will create a program to print numbers from 1 to 10 with the help of goto statement.
// C program to print numbers
// from 1 to 10 using goto statement
#include <stdio.h>
int main ()
{
int n = 1;
label:
printf ("%d \n", n);
n++;
if (n<=10)
goto label;
return 0;
}
Output -:
1
2
3
4
5
6
7
8
9
10
Friends, you even learned what is goto statement and how to use goto statement in c language.
But using goto statement in any program can make the program a bit complex. In such a situation, you should know when it is right for you to use goto statement in the program.
So let’s know when goto statement should be used in the program.
When should we use goto statement?
By the way, we can use goto statement anytime under any condition, but it is right to use it when we have to come out of a loop at once.
See the program below
#include <stdio.h>
int main ()
{
int i, j, k;
for (i = 0; i<10; i++)
{
for (j = 0; j<5; j++)
{k = 0;
while (k<3)
{
printf ("%d %d %d \n", i, j, k);
if (j == 3)
{
goto out;
}
k++;
}
}
}
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
0 2 1
0 2 2
0 3 0
came out of the loop
look at that program above. We have used many loop statements in that program. To get out of all these loop statements at once, we used goto statement.
If we think of doing this with the help of the break keyword, then do not address it because the break keyword is only used to come out from the same loop and we have to come out from the whole loop at once, then for that, we have to do use the goto statement
Read More -:
- Control Statements in C
- If else Statement in C
- Switch Statement in C
- Loop in C
- For Loop in C
- While and Do while Loop in C
- Break Statement in C
- Continue statement in C
- Conditional Operator in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, I hope that after reading this post today, you have learned what is goto statement in C Language And how we use it in C language
Friends, I hope you have found the answer of your question and you will not have to search about the goto statement in C language.
However, if you want any information related to this post or related to programming language, and computer science, then comment below I will clear your all doubts.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the goto statement in C Language.
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.