4.2.2 do...while 循环语句
作用:满足循环条件,执行循环语句
语法:do{ 循环语句 } while{ 循环条件 };
注意:与while的区别在于do...while 会先执行一次训话语句,在判断循环条件

image.png
示例;
#include <iostream>
using namespace std;
int main()
{
//do..while语句
//在屏幕中输出0-9
int num = 0;
do
{
cout << num << endl;
num++;
} while (num<10);
//do..while和while循环区别,前者先执行在判断
system("pause");
return 0;
}
练习案例:水仙花数
案例描述水仙花数是指一个三位数,他的每个位上的数字的3次幂之和等于它本身
例如:13+53+3^3 = 153
示例:
#include <iostream>
using namespace std;
int main()
{
//1、先打印所有三位数
int num = 100;
do
{
int a = 0;
int b = 0;
int c = 0;
a = num % 10; //取个位
b = num / 10 % 10;//取十位
c = num / 100;//取百位
//2、从所有三位数中找到水仙花数
if (a*a*a+b*b*b+c*c*c == num)
{
cout << "三位数中的水仙花数:" << num << endl;
}
num++;
} while (num < 1000);
system("pause");
return 0;
}