GOTO
In C programming, goto statement is used for altering the
normal sequence of program execution by transferring control to some other part
of the program.
Syntax of goto
statement
Goto lable:
……………….
……………….
……………….
Lable:
Statement;
In this syntax, label is an identifier. When, the control
of program reaches to goto statement, the control of the program will jump to
the label: and executes the codes after it.
|
/* C program to demonstrate the working of goto
statement.*/
#include<stdio.h>
Int main()
{
Float num,
average, sum;
Int I,n;
Printf(“Maximum
no. of inputs:”);
Scanf(“%d”,&n);
For(i=0;i<=n;i++)
{
Printf(“enter n%d”,i);
Scanf(“%F”,&num);
If(num<0.0)
Goto jump;
Sum=sum+num;
}
Jump:
Average=sum/(i-1);
Printf(“Average:%.2f”,average);
Return 0;
}
Output:
Maximum no. of inputs:4
Enter n1: 1.5
Enter n2:12.5
Enter n3:7.2
Enter n4: -1
Average : 7.07
Remark: Though goto statement is included in ANSI
standard of C, use of goto statement should be reduced as much as possible in
a program.
|
No comments:
Post a Comment