goto statement

goto statement:

goto allow to transfer program control any where within the function.
Syntax: goto label;
it transfer program control at specified label statement label can be define by using

Syntax: identifier

goto

Example
# include <iostream.h>

int main()

{

float num, average, sum = 0.0; int i, n;

cout<<“Maximum number of inputs: “;

cin>>n;

for(i=1; i <= n; ++i)

{

cout<<“Enter n”<<i<<“: “;

cin>>num; if(num < 0.0)

{

goto jump; /* Control of the program moves to jump; */

}

sum += num;

}

jump: average=sum/(i-1);

cout<<“\nAverage = “<<average; return 0;

}

Output

Maximum number of inputs:3

Enter n1:23

Enter n2:34

Enter n3:45

Average =34

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *