循环语句
while循环
do_while循环
do
{
}while(表达式);
do
{
}while(0);
- while循环与do while 循环区别
- while循环有可能一次循环也不会执行,do while 循环不管怎么样都会执行一次循环
for循环
for(表达式1;表达式2;表达式3)
{
表达式4;
}
for(;;);
for(;1;);
for(;;)
跳转语句
循环语句的使用
- 循环一般都是用来描述有规律的问题,比如推导公式类的和递推关系类的
#include <stdio.h>
int main()
{
int i = 0;
int temp = 1;
int iresult = 0;
for (i = 1; i <= 5; i++)
{
temp *= i;
printf("%d\t%d\n", temp, i);
iresult += temp;
}
printf("iresult=%d\n", iresult);
return 0;
}
#include <stdio.h>
int main()
{
double result = (double)1 / 2 + (double)2 / 3 + (double)3 / 4 + (double)4 / 5;
printf("result=%.2lf\n", result);
result = 0.0;
for (int i = 1; i < 5; i++)
{
result += (double)i / (i + 1.0);
}
printf("result=%.2lf\n", result);
}
#include <stdio.h>
int main() {
double s = 1.0;
int n, i = 3;
scanf("%d", &n);
for (i = 3; i <= n; i += 2)
s += 1.0 / i;
printf("s=%.2f", s);
return 0;
}
循环嵌套
|