Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรม C อธิบายคำสั่ง goto Statement

โปรแกรม C คำนวณรากที่สองของ 5 ตัวเลข ตัวแปร count นับจำนวนที่อ่านแล้ว เมื่อ count <= 5 goto read นำการควบคุมกลับไปที่ label read มิฉะนั้นพิมพ์ข้อความและหยุด

Goto Statement

ใช้เปลี่ยนลำดับการทำงานปกติโดยโยนการควบคุมไปส่วนอื่นของโปรแกรม

โปรแกรม C อธิบายคำสั่ง goto Statement

โปรแกรม

โปรแกรม C การใช้งาน goto statement:

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