C 程序计算五个数字的平方根。变量 count 存储读取的数字的计数。当count小于或等于5时,goto read语句将控制指向读取的标签。否则,程序打印一条消息并停止。
它在正常的程序执行顺序之后使用,将控制权转移到程序的其他部分。
以下是使用 goto 语句的 C 程序 -
#include <math.h> main(){ double x, y; int count; count = 1; printf("Enter FIVE real values in a LINE"); read: scanf("%lf", &x); printf("
"); if (x < 0) printf("Value - %d is negative
",count); else{ y = sqrt(x); printf("%lft %lf
", x, y); } count = count + 1; if (count <= 5) goto read; printf("
End of computation"); }
当执行上述程序时,会产生以下结果 -
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