4.2.2 循环结构-do while

4.2.2 循环结构-do while

语法:
do
{
[循环体]
}while([逻辑表达式])
当逻辑表达式为真时退出循环。
示例:

#include
using namespace std;
int main() 
{
     
	//do while 先执行再判断条件
	int num = 0;
	do
	{
     
		cout << num << '\t';
		num++;
	} while (num < 10);
	cout << endl;
	system("pause");
	return 0;
}

运行结果:
4.2.2 循环结构-do while_第1张图片
再看一个水仙花数的案例:
水仙花数是:个位3+十位3+百位3=这个数本身。

#include
using namespace std;
int main()
{
     
	//输出所有的水仙花数(100-999)
	int num = 100;
	do
	{
     
		int a = num % 10;		//个位
		int b = num / 10 % 10;	//十位
		int c = num / 100;		//百位
		if (a * a * a + b * b * b + c * c * c == num)
		{
     
			cout << num << endl;
		}
		num++;
	} while (num < 1000);
	system("pause");
	return 0;
}

运行结果:
4.2.2 循环结构-do while_第2张图片

你可能感兴趣的:(C++完整学习笔记,c++,编程语言)