用案例说明三种循环的差异或循环条件控制的方法
来源:学生作业帮 编辑:搜狗做题网作业帮 分类:综合作业 时间:2024/08/18 17:05:49
用案例说明三种循环的差异或循环条件控制的方法
![用案例说明三种循环的差异或循环条件控制的方法](/uploads/image/z/13085501-5-1.jpg?t=%E7%94%A8%E6%A1%88%E4%BE%8B%E8%AF%B4%E6%98%8E%E4%B8%89%E7%A7%8D%E5%BE%AA%E7%8E%AF%E7%9A%84%E5%B7%AE%E5%BC%82%E6%88%96%E5%BE%AA%E7%8E%AF%E6%9D%A1%E4%BB%B6%E6%8E%A7%E5%88%B6%E7%9A%84%E6%96%B9%E6%B3%95)
#include <stdio.h>
#define MAXIMUM10
int main()
{
int i, j, k;
int sum1 = 0, sum2 = 0, sum3 = 0;
// for loop example
for (i = 1; i < MAXIMUM; i++)
{
sum1 += i;
}
printf("for loop example: sum1 is: %d\n", sum1);
// while loop example, when j is more or equal than 10, the while loop don't the loop
j = 1;
while (j < MAXIMUM)
{
sum2 += j++;
}
printf("while loop example: sum1 is: %d\n", sum2);
// do while loop example, when k is more or equal than 10, the do while will do once loop
k = 1;
do
{
sum3 += k++;
} while (k < MAXIMUM);
printf("do while loop example: sum1 is: %d\n", sum3);
return 0;
}
#define MAXIMUM10
int main()
{
int i, j, k;
int sum1 = 0, sum2 = 0, sum3 = 0;
// for loop example
for (i = 1; i < MAXIMUM; i++)
{
sum1 += i;
}
printf("for loop example: sum1 is: %d\n", sum1);
// while loop example, when j is more or equal than 10, the while loop don't the loop
j = 1;
while (j < MAXIMUM)
{
sum2 += j++;
}
printf("while loop example: sum1 is: %d\n", sum2);
// do while loop example, when k is more or equal than 10, the do while will do once loop
k = 1;
do
{
sum3 += k++;
} while (k < MAXIMUM);
printf("do while loop example: sum1 is: %d\n", sum3);
return 0;
}