Home  >  Article  >  Backend Development  >  C program explains goto statement

C program explains goto statement

WBOY
WBOYforward
2023-09-11 10:41:011002browse

C program calculates the square roots of five numbers. The variable count stores the count of numbers read. When count is less than or equal to 5, the goto read statement will control the label pointed to the read. Otherwise, the program prints a message and stops.

Goto statement

It is used after the normal program execution sequence to transfer control to other parts of the program.

C program explains goto statement

Program

The following is a C program using goto statement-

#include <math.h>
main(){
   double x, y;
   int count;
   count = 1;
   printf("Enter FIVE real values in a LINE </p><p>");
   read:
   scanf("%lf", &x);
   printf("</p><p>");
   if (x < 0)
      printf("Value - %d is negative</p><p>",count);
   else{
      y = sqrt(x);
      printf("%lf\t %lf</p><p>", x, y);
   }
   count = count + 1;
   if (count <= 5)
      goto read;
   printf("</p><p>End of computation");
}

Output

When executed When executing the above program, the following results will be produced -

Enter FIVE real values in a LINE
2.3 -4.5 2 6.8 -44.7
2.300000 1.516575
Value - 2 is negative
2.000000 1.414214
6.800000 2.607681
Value - 5 is negative
End of computation

The above is the detailed content of C program explains goto statement. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete