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-08-27 18:58:22 发布
通过对比分析for循环和while循环的goto语句版本代码,可以看出这两种循环结构在机器内部是等效的,可以互相替换。示例代码分别展示了如何用goto语句实现for循环和while循环的累加及阶乘计算。
1925

被折叠的 条评论
为什么被折叠?



