The goto statement is used to transfer control to a specified label. However, the label must reside in the same function and can appear only before one statement in the same function.
goto
STATEMENT
The
goto statement is used to transfer control to a specified label. However, the
label must reside in the same function and can appear only before one statement
in the same function. The syntax of goto statement is shown in Figure 3.11.
Here,
label is an identifier that specifies the place where the branch is to be made.
Label can be any valid variable name that is followed by a colon (:). The label
is placed immediately before the statement where the control has to be transferred.
The
label can be placed anywhere in the program either before or after the goto
statement. Whenever the goto statement is encountered the control is
immediately transferred to the statements following the label. Therefore, goto
statement breaks the normal sequential execution of the program. If the label
is placed after the goto statement, then it is called a forward jump and in case it is located before the goto statement,
it is said to be a backward jump.
The
goto statement is often combined with the if statement to cause a conditional
transfer of control.
IF condition THEN goto label
In
this book, we will not use the goto statement because computer scientists
usually avoid this statement in favour of the 'structured programming'
paradigm. Some scientists think that the goto statement should be abolished
from higher-level languages because they complicate the task of analysing and
verifying the correctness of programs (particularly those involving loops).
Programming Tip:
Follow
proper indentation for better clarity, readability, and understanding of the
loops.
Moreover,
structured program theory proves that the availability of the goto statement is
not necessary to write programs, as combination of sequence, selection, and repetition
constructs is sufficient to perform any computation. The code given below
demonstrates the use of a goto statement. The program calculates the sum of all
positive numbers entered by the user.
#include <stdio.h>
int main()
{
int num, sum=0;
read: // label for goto statement
printf("\n Enter the number.
Enter 999 to end: ");
scanf("%d", &num);
if (num != 999)
{
if (num < 0)
goto read; // jump to label- read
sum += num;
goto read; // jump to label- read
}
printf("\n Sum of the numbers
entered by the user is = %d", sum);
return 0;
}
Conclusion
•
It is not necessary to use goto statement as it can always be eliminated by
rearranging the code.
•
Using the goto statement violates the rules of structured programming.
•
It is a good programming style to use break, continue, and return statements in
preference to goto whenever possible.
•
Goto statements make the program code complicated and renders the program
unreadable.
Note
One
must avoid the use of break, continue, and goto statements as much as possible
as they are techniques used in unstructured programming.
In
structured programming, you must prefer to use if and if-else constructs
to avoid such statements. For example, look at the following code which
calculates the sum of numbers entered by the user. The first version uses the
break statement. The second version replaces break by if-else construct.
// Uses break statement
#include <stdio.h>
int main()
{
int num, sum=0;
while (1)
{
printf("\n Enter any number.
Enter 999 to stop: ");
scanf("%d", &num);
if (num==999)
break; // quit the loop
sum+=num;
}
printf("\n SUM = %d",
sum);
return 0;
}
// Same program without using break
statement
#include <stdio.h>
int main()
{
int num, sum=0, flag=1; // flag
will be used to exit from the loop
while (flag = =1) // loop control
variable
{
printf("\n Enter any number.
Enter 999 to stop: ");
scanf("%d", &num);
if (num! 999)
sum+=num;
else
flag=0; // to quit the loop
{
printf("\n SUM= %d",
sum);
return 0;
}
Now
let us see how we can eliminate continue statement from our programs. Let us
first write a program that calculates the average of all non-zero numbers
entered by the user using the continue
statement. The second program will do the same job but without using continue.
#include <stdio.h>
int main()
{
int num, sum=0, flag=1, count=0;
float avg;
// flag will be used to exit from
the loop
while (flag==1)
{
printf("\n Enter any number.
Enter 999 to stop: ");
scanf("%d", &num);
if (num==0)
continue; // skip the following
statements
if (num!=999)
{
sum+=num;
count++;
}
else
flag=0;
// set loop cntl var to jump out of
loop
}
printf("\n SUM %d", sum);
avg = (float) sum/count;
printf("\n Average %f",
avg);
return 0;
}
// Same program without using
continue statement
#include <stdio.h>
int main()
{
int num, sum=0, flag=1, count=0;
float avg; // flag will be used to
exit from the loop
while (flag = =1)
{
printf("\n Enter any number.
Enter 999 to stop: ");
scanf("%d", &num);
if (num!=0)
{
if (num!=999)
{
sum+=num;
count++;
}
else
flag=0;
}
}
printf("\n SUM= %d",
sum);
avg = (float) sum/count;
printf("\n Average=%f",
avg);
return 0;
}
Programming in C: Unit I (c): Decision Control and Looping Statements : Tag: : with Example C Programs - goto Statement
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation