while循环和do-while循环
程序员文章站
2023-12-25 22:28:39
...
1、while循环
#include <iostream>
int main()
{
int i = 1, sum = 0;
while(i<=100)
{
sum += i;
i++;
}
printf("1-100所有数字相加 = %d\n", sum);
return 0;
}
#include <iostream>
int main()
{
int n = 0;
printf("请输入一个字符串:");
while(getchar()!='\n')
{
n++;
}
printf("一共有%d个字符\n", n);
return 0;
}
2、do-while循环
#include <iostream>
int main()
{
int i = 1, sum = 0;
do {
sum += i;
i++;
} while (i <= 100);
printf("1-100相加的值为:%d\n", sum);
}