# 1 while循环语句

int main()
{
//打印0~9
int num = 0;
while (num < 10) {
cout << num << endl;
num += 1;
}
system("pause");
return 0;
}


## while循环练习案例：猜数字

#include <iostream>
#include<stdlib.h>
using namespace std;
#include<ctime>
//猜数字
int main()
{
//添加随机数种子
srand((unsingne int)time(NULL));
//生成随机数
int num = rand() % 100 +1 ; //rand()%100 生成一个0~99的随机数
int val = 0;
while (1) {
cin >> val;
if (val > num) {
cout << "大了" << endl;
}
else if (val < num) {
cout << "小了" << endl;
}
else {
cout << "恭喜你猜对" << endl;
break;//break,退出当前循环
}
}
system("pause");
return 0;
}


# 2 do……while循环语句

#include<iostream>
using namespace std;

int main()
{
//do...while 循环打出0~9
int num = 0;
do
{
cout << num << endl;
num++;
}
while(num<10);

system("pause");
return 0;
}


## 练习案例：水仙花数

#include<iostream>
using namespace std;
#include<math.h>
int main()
{
//do...while 水仙花数
int num = 100;
int ge, shi, bai,sum;
do
{
ge = num % 10;
bai = num / 100;
shi = (num / 10) % 10;
sum = pow(ge, 3) + pow(shi, 3) + pow(bai, 3);
if (sum==num)
{
cout << num << endl;
}
num++;
}
while(num<1000);

system("pause");
return 0;
}


# 3 for循环语句

int main()
{
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}

system("pause");
return 0;
}


## 练习案例：逢7敲桌子

int main()
{
//逢7
for (int i=1;i<100;i++)
{
if (i % 10 == 7 || i / 10 == 7 || i % 7 == 0)
{
continue;
}
cout << i << endl;

}

system("pause");
return 0;
}


# 4 嵌套循环

## 4.1 打印10*10星星

#include<iostream>
using namespace std;
#include<math.h>
int main()
{
//打印10行
for (int i = 0; i < 10; i++)
{
//打印一行*
for (int j = 0; j < 10; j++)
{
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}


## 4.2用代码写出乘法口诀表

#include<iostream>
using namespace std;
#include<math.h>
int main()
{
//乘法口诀表
for (int hang = 1; hang < 10; hang++)
{
int lie = 1;
do
{
cout << lie << "*" << hang << "=" << lie * hang << "\t";
lie++;

} while (lie<=hang);
cout << endl;
}
system("pause");
return 0;
}


12-27
08-04 5226

02-27 119
02-18 480
05-19 2万+
01-10 2万+
02-18 51