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

C program explains goto statement

WBOY
Release: 2023-09-11 10:41:01
forward
1101 people have browsed it

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");
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template