Introduction: In this article i
will explain with a program what is goto or we can say labelled statement, what are its advantages and disadvantages and how to calculate sum of two numbers and prompting
whether to continue with more calculations or not using goto statement.
Description: In previous articles
i explained the program How to find greatest of two numbers using if else statement in C Language and Get number of days in month and year using else if ladder and Else if ladder to perform operation on two numbers based on operator and How to find greatest of two numbers using conditional operator .
In this program i am going to explain what goto
statement is and how to use it. Goto statement is used to alter the normal sequence of program execution by transferring control of execution to some other part of the program unconditionally. So the control of flow transfers unconditionally to the statement
specified by the label of goto statement.
Syntax for goto:
goto label;
statement 2;
.
.
.
label:statement n;
Here goto is a keyword and label
can be any valid identifier.
In above syntax it is clear that
after the execution of goto statement the control will jump unconditionally
from there to statement-n which is prefixed by the label, without executing in between
statements.
Advantage:
using goto statement you can alter the normal sequence of the program
execution so it gives the power to jump to any part of program.
Disadvantages:
It is always recommended not to
use goto statement as this reduces the readability of the program. It becomes
difficult to trace the control flow of a program, making the program logic
complex to understand .Using goto statement is considered as poor programming
practice. Any program written in C language can be written without the use of
goto statement. So try to avoid goto statement as possible as you can.
There are two types of jump in
goto statement:
1. Forward jump
2. Backward jump
In the example program below both
the forward jump and backward jump is used.
Implementation: Let's create the
program.
#include<conio.h>
#include<stdio.h>
main()
{
int a,b;
char ch;
clrscr();
y:printf("Enter any two numbers=
");
scanf("%d%d",&a,&b);
printf("First number= %d\n",a);
printf("Second number=
%d\n",b);
printf("Sum of %d and %d = %d\n", a,b,a+b);
printf("\nDo you want more
calculations? Enter y for Yes and n for No= ");
scanf(" %c",&ch);
if(ch=='y')
{
goto y;
}
else if(ch=='n')
{
goto n;
}
else
{
printf("Invalid choice...enter y for Yes and n for No");
}
n:printf("Please press enter to
exit...HAVE A NICE DAY AHEAD!!!");
getch();
}
run the program using ctrl+F9
Output:
Enter any two numbers= 11
22
First number= 11
Second number= 22
Sum of 11 and 22 = 33
Do you want more calculations? Enter
y for Yes and n for No= y
Enter any two numbers= 56
88
First number= 56
Second number=88
Sum of 56 and 88 = 144
Do you want more calculations? Enter
y for Yes and n for No= n
Please press enter to exit...HAVE
A NICE DAY AHEAD!!!");
Now over to you:
"If you like my work; you
can appreciate by leaving your comments, hitting Facebook like button,
following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on
stumble upon and subscribing for receiving free updates directly to your inbox
. Stay tuned for more technical updates."
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..