Go to Statement in C.
Hi Everyone,
Today we will discuss about go to statement in C which is rarely used in Program but sometimes we need to use this to jump statement in C. Before going through this blog please go through my previous C blogs for your reference so that you can easy to understand the program . You will find the blogs in archive folder from below blog:
dheeraj60.blogspot.com
Let’s start with go to statement in C.
goto statement in C is known as jump statement in C. Go to statement is used to transfer the program control to a predefined . go to also be used to break the multiple loops which can't be done by using a single break statement. Go to statement is rarely used in program because it makes program confusing and complex as the control of the program difficult to trace, so for testing it’s bit confusing.
Defining go to we need to put label name and can use it in program
Syntax of go to statement:
label:
// part of code;
goto label;
Let’s understand go to statement through one program table where we can pass input from user and take the output by using go to statement.
Below is the Program :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int tab,i=1;
printf(" Please enter the number for table\n");
scanf("%d",&tab);
table:
printf("%d x %d = %d\n",tab,i,tab*i);
i++;
if(i<=10)
goto table;
return 0;
}
Here table is label and when the value of condition (i<=10) then statements jumps to label table and it will run till 10 times and give the output pass by the user.
Below is the output of above program in compiler code block.
Let’s take another example of addition of number by using go to statement in this program. In this program we use the same as above program logic , After this program we will see how go to statements works in multiple loops.
Below is the Program .
#include <stdio.h>
#include <stdlib.h>
int main()
{
int add=0;
for(int i = 0; i<=10; i++){
add = add+i;
if(i==10){
goto addition;
}
}
addition:
printf("%d \n", add);
return 0;
}
In this example, we used label addition and when the value of i (inside loop) is equal to 10 then we are jumping to go to addition label. This is reason the sum is displaying the sum of numbers till 10 even the loop is set to run from 0 to 10.
and after each iteration of loop add=add+i; so here we have given condition if i equals to 10 so output will be 55 and if we give the condition i equals to 5 it will give 15 you can check with other number also.
Below is the output of above program in Code Block Compiler.
Hope you are able to understand go to statement. Now go to statement we can say is good when we use multiple loops in a program as go to break the multiple loops using a single statement at the same time.
Let’s see the below program where we use multiple loops with go to statement.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, k;
for (i=0; i<10; i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
printf("%d %d %d\n",i,j,k);
if(j == 2)
{
goto terminate;
}
}
}
}
terminate:
printf("terminate from the loop");
return 0;
}
Here we can see how go to is useful as we use multiple loops in the program and mark label here terminate and through condition we can come out from the loops as per condition , we can give any condition as per requirement and it’s work better than break statement in this case .
Below is the output of above program in compiler code block for your reference.
In next blog I will come up with uses of String in C program and some useful programs in C before i am going to start function and Pointer in C program.
Thanks.
Comments
Post a Comment