首頁  >  文章  >  後端開發  >  C程式解釋goto語句

C程式解釋goto語句

WBOY
WBOY轉載
2023-09-11 10:41:011003瀏覽

C 程式計算五個數字的平方根。變數 count 儲存讀取的數字的計數。當count小於或等於5時,goto read語句將控制指向讀取的標籤。否則,程式會列印一條訊息並停止。

Goto 語句

它在正常的程式執行順序之後使用,將控制權轉移到程式的其他部分。

C程式解釋goto語句

程式

以下是使用goto 語句的C 程式-

#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");
}

輸出

當執行上述程序時,會產生以下結果-

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

以上是C程式解釋goto語句的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除