迭代-while循环
#include<stdio.h>
//while循环
int whileLoop(int n){
int res = 0;
int i = 1;//初始化条件变量
//循环求和:1+2+3+...+n
while (i <= n){
res += i;
i++;
}
return res;
}
int main(){
int n = 5;
int res = whileLoop(n);
printf("%d", res);
}
//for 循环的代码更加紧凑,while 循环更加灵活,两者都可以实现迭代结构。
//选择使用哪一个应该根据特定问题的需求来决定。
浙公网安备 33010602011771号