循环结构
while循环语句
作用:满足循环条件,执行循环语句
语法:while (循环条件) {循环语句};
解释:只要循环条件的结果为真,就执行循环语句
#include<iostream>
using namespace std;
int main8()
{
//while循环
//在屏幕中打印0~9这10个数字
int num = 0;
//注意事项:在写循环时一定要避免死循环的出现
while (num <= 9)
{
cout << num << endl;
num++;
}
system("pause");
return 0;
}
案例分析
#include<iostream>
using namespace std;
#include<ctime>
int main9()
{
//添加随机数种子,作用是利用当前系统时间生成随机数,防止每次生成的随机数都一样
srand((unsigned int) time (NULL));
//1.系统生成随机数
int num = rand() % 100 + 1; //1~100
//cout << num << endl;
//2.玩家进行猜测
int val = 0; //玩家输入的数据
cout << "请输入您猜测的数据:" << endl;
while (1)
{
cin >> val;
//3.判断玩家的猜测
if (val > num)
{
cout << "猜测过大" << endl;
}
else if (val < num)
{
cout << "猜测过小" << endl;
}
else
{
cout << "恭喜您猜对了" << endl;
break; //利用break关键字退出当前循环
}
}
//猜对 退出游戏
//猜错 提示猜的结果 过大或者过小 重新返回第二步
system("pause");
return 0;
}
dowhile循环语句
作用:满足循环语句,执行循环语句
语法:do {循环语句} while (循环条件);
注意:与while的区别在于do...while...会先执行一次循环语句,再判断循环条件
#include<iostream>
using namespace std;
int main10()
{
//dowhile循环
//在屏幕中打印0~9这10个数字
int num = 0;
do
{
cout << num << endl;
num++;
} while (num <= 9);
//dowhile循环和while循环的区别在于dowhile会先执行一次循环语句
system("pause");
return 0;
}
dowhile水仙花数
#include<iostream>
using namespace std;
int main11()
{
//水仙花数
//水仙花数是指一个三位数,它的每个位上的数字的三次幂之和等于它本身
//例如:1^3 + 5^3 + 3^3 = 153
//1.将所有三位数进行输出(100~999)
int num = 100;
do
{
//2.在所有三位数中找到水仙花数
//判断:个位^3 + 十位^3 + 百位^3 = 本身
int a = 0;
int b = 0;
int c = 0;
a = num % 10;
b = num / 10 % 10;
c = num / 100;
if ((a*a*a + b*b*b + c*c*c) == num)
{
cout << num << endl;
}
num++;
} while (num <= 999);
system("pause");
return 0;
}
for循环语句
作用:满足循环条件,执行循环语句
语法:for (起始表达式;条件表达式;末尾循环体) {循环语句};
#include<iostream>
using namespace std;
int main12()
{
//for循环
//在屏幕中打印0~9这10个数字
for (int i = 0; i <= 9; i++)
{
cout << i << endl;
}
system("pause");
return 0;
}
敲桌子
#include<iostream>
using namespace std;
int main13()
{
//敲桌子
//从1开始数到数字100,如果数字个位含有7,或者数字十位含有7,或者该数是7的倍数,我们打印敲桌子,其余数字直接输出
for (int i = 1; i <= 100; i++)
{
if (i % 7 == 0 || i / 10 == 7 || i % 10 == 7) //特殊数字
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
system("pause");
return 0;
}
嵌套循环
作用:在循环中再嵌套一层循环,解决一些实际问题
#include<iostream>
using namespace std;
int main14()
{
//打印一行星图
//外层执行一次,内层执行一周
//外层循环
for (int i = 0; i <= 9; i++) {
//内层循环
for (int j = 0; j <= 9; j++)
{
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
案例:乘法口诀表
#include<iostream>
using namespace std;
int main15()
{
//乘法口诀表
//外层循环
for (int i = 1; i <= 9; i++) {
//内层循环
for (int j = 1; j <= i; j++)
{
cout << j << " * " << i << " = " << j*i << "\t";
}
cout << endl;
}
system("pause");
return 0;
}
跳转语句
break语句
作用:跳出选择结构或者循环结构
break使用时机:出现在switch语句中,作用是终止case并跳出switch
出现在循环语句中,作用是跳出当前的循环语句
出现在嵌套语句中,跳出最近的内存循环语句
#include<iostream>
using namespace std;
int main16()
{
//跳转语句break
//出现在switch语句中,作用是终止case并跳出switch
cout << "请选择副本难度" << endl;
cout << "1.普通" << endl;
cout << "2.中等" << endl;
cout << "3.困难" << endl;
int select = 0;
cin >> select;
switch (select)
{
case 1:
cout << "您选择的是普通难度" << endl;
break;
case 2:
cout << "您选择的是中等难度" << endl;
break;
case 3:
cout << "您选择的是困难难度" << endl;
break;
default:
break;
}
//出现在循环语句中,作用是跳出当前的循环语句
for (int i = 0; i <= 9; i++)
{
//如果i等于5,退出循环,不再打印
if (i == 5)
{
break; //退出循环
}
cout << i << endl;
}
//出现在嵌套语句中,跳出最近的内存循环语句
for (int i = 0; i <= 9; i++)
{
for (int j = 0; j <= 9; j++)
{
//如果j等于5,退出循环,不再打印
if (j == 5)
{
break;
}
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
continue语句
作用:在循环语句中,跳过本次循环中余下尚未执行的语句,继续下一次循环
#include<iostream>
using namespace std;
int main17()
{
//跳转语句continue
for (int i = 0; i < 100; i++)
{
//如果是奇数就输出,偶数不输出
if (i % 2 == 0)
{
continue; //可以筛选条件,执行到此就不再向下执行,执行下一次循环
//break; 直接跳出循环
}
cout << i << endl;
}
system("pause");
return 0;
}
goto语句
作用:可以无条件跳转语句
语法:goto标记
解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
#include<iostream>
using namespace std;
int main18()
{
//跳转语句goto
cout << "1.xxxxx" << endl;
cout << "2.xxxxx" << endl;
goto FLAG;
cout << "3.xxxxx" << endl;
cout << "4.xxxxx" << endl;
FLAG:
cout << "5.xxxxx" << endl;
system("pause");
return 0;
}
|