for循环的goto语句版本的代码和while循环的goto语句版本的代码几乎一样.也就是说,这两种循环的控制结构是一样的.
由此可知,在机器内部for循环和while循环是等效的,二者之间可以相互替换.
/**************************************************/
/* for_goto.c使用goto语句实现fot循环 /
//
#include <stdio.h>
int main(void)
{
int n = 1;
int add = 0;/累加和/
int num = 0;
int t;
scanf("%d,", &num);
loop:
add += n;
t = n == num;
if(t)
{
goto done;
}
n++;
goto loop;
done:
printf("%d", add);
return 0;
}
//
/ while_goto.c使用goto语句实现while循环 /
/************************************************/
#include <stdio.h>
int main(void)
{
int n = 1;
int mul = 1;/阶乘/
int num = 0;
int t;
scanf("%d,", &num);
loop:
mul *= n;
t = n == num;
if(t)
{
goto done;
}
n++;
goto loop;
done:
printf("%d\n",mul);
return 0;
}
笔记:for循环与while循环可以相互替换
最新推荐文章于 2025-03-25 13:36:50 发布