Goto Statement

Goto statement:

It is used to transfer the normal flow of a program to the given label in the program.
• the syntax for goto statements
{
…….
go to label;
…….
…….
LABEL:
statements;
}

goto

 

Example
#include<stdio.h>

int main()

{

int p;

for(p=0;p<10;p++)

{

if(p==5)

{

printf(“\nWe are using goto statement when p = 5″);

goto HAI1;

}

printf(“%d “,p);

}

HAI1 : printf(“\nNow, we are inside label name \”hai1\” \n”);

}

Output
0 1 2 3 4

We are using goto statement when p = 5

Now, we are inside label name “hai1”

 

WAP to calculate the sum and average of maximum of 5 numbers , If user enters negative numer, the sum and averbage of previously entered positive number is display

# include<stdio.h>

int main()

{

const int maxInput1 = 5;

int p;

double number1, average1, sum1=0.0;

for(p=1; p<=maxInput; ++p)

{

printf(“%d. Enter a number: “, p);

scanf(“%lf”,&number);

if(number1 < 0.0)

goto jump;

sum1 += number1; // sum = sum+number; }

jump: average1=sum1/(i-1);

printf(“Sum = %.2f\n”, sum1);

printf(“Average = %.2f”, average1);

return 0;

}

Output

1. Enter a number: 3

2. Enter a number: 4.3

3. Enter a number: 9.3

4. Enter a number: -2.9

Sum = 16.60

Leave a Reply

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