循环结构
while循环语句
作用:满足循环条件,执行循环语句
语法:while(循环条件){ 循环语句 }
只要循环条件的结果为真,就执行循环语句
示例:
int main() {
int num = 0;
while (num < 10)
{
cout << "num = " << num << endl;
num++;
}
system("pause");
return 0;
}
在执行循环语句时候,程序必须要提供跳出循环的出口,否则会出现死循环。
while循环练习案例:猜数字
案例描述:系统随机生成一个1到100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果猜对恭喜玩家胜利,并且退出游戏。
代码
#include <iostream>
using namespace std;
int main() {
int a = rand() % 100;
int b = 0;
while (1) {
cout << "请猜数字:";
cin >> b;
if(b>a){
cout << "猜大了"<<endl;
}
else if (b < a) {
cout << "猜小了"<<endl;
}
else {
cout << "猜对了";
break;
}
}
return 0;
运行结果:

do…while循环语句
作用: 满足循环条件,执行循环语句
语法: do{ 循环语句 } while(循环条件);
示例;
int main() {
int num = 0;
do
{
cout << num << endl;
num++;
} while (num < 10);
system("pause");
return 0;
}
与while循环区别在于,do…while先执行一次循环语句,再判断循环条件
练习案例:水仙花数
案例描述:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身
例如:1^3 + 5^3+ 3^3 = 153
用do…while语句,求出所有3位数中的水仙花数
代码如下
#include <iostream>
using namespace std;
int main() {
int num = 100;
cout << "三位数的水仙花数有:"<<endl;
do {
int ge = num % 100 %10;
int shi = num % 100 / 10;
int bai = num / 100;
int a = (bai * bai * bai) +( shi * shi * shi) + (ge * ge * ge);
if (a == num) {
cout << num<<endl;
}
num++;
} while (num<1000);
return 0;
}
结果如下:

for循环语句
作用: 满足循环条件,执行循环语句
语法:for(起始表达式;条件表达式;末尾循环体) { 循环语句; }
示例:
int main() {
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
system("pause");
return 0;
}
for循环中的表达式,要用分号进行分隔
练习案例:敲桌子
从1开始数到数字100, 如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出。
代码:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i <= 100;i++) {
int ge = i % 10;
int shi = i / 10;
int a = i % 7;
if (ge == 7 || shi == 7 || a == 0) {
cout << "敲桌子" << endl;
}
else {
cout << i<<endl;
}
}
return 0;
}
结果

总结:while , do…while, for都是开发中常用的循环语句,for循环结构比较清晰,比较常用
嵌套循环
作用: 在循环体中再嵌套一层循环,解决一些实际问题
例如我们想在屏幕中打印如下图片,就需要利用嵌套循环
int main() {
//外层循环执行1次,内层循环执行1轮
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << "*" << " ";
}
cout << endl;
}
system("pause");
return 0;
}
乘法口诀表,利用嵌套循环,实现九九乘法表;
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 9;i++) {
for (int j = 1; j <= i; j++) {
cout << j << "*" << i << "=" << i * j << " ";
}
cout << endl;
}
}
结果

break语句
作用: 用于跳出选择结构或者循环结构
break使用的时机:
出现在switch条件语句中,作用是终止case并跳出switch
出现在循环语句中,作用是跳出当前的循环语句
出现在嵌套循环中,跳出最近的内层循环语句
int main() {
//1、在switch 语句中使用break
cout << "请选择您的爱好:" << endl;
cout << "1、读书" << endl;
cout << "2、唱歌" << endl;
cout << "3、打篮球" << endl;
int num = 0;
cin >> num;
switch (num)
{
case 1:
cout << "您的爱好是读书" << endl;
break;
case 2:
cout << "您的爱好是唱歌" << endl;
break;
case 3:
cout << "您的爱好是跳舞" << endl;
break;
}
system("pause");
return 0;
}
continue语句
**作用:**在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环
示例:
int main() {
for (int i = 0; i < 100; i++)
{
if (i % 2 == 0)
{
continue;
}
cout << i << endl;
}
system("pause");
return 0;
}
goto语句
作用:可以无条件跳转语句
语法: goto 标记;
如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
int main() {
cout << "1" << endl;
goto FLAG;
cout << "2" << endl;
cout << "3" << endl;
cout << "4" << endl;
FLAG:
cout << "5" << endl;
system("pause");
return 0;
}
在程序中不建议使用goto语句,以免造成程序流程混乱
本文详细介绍了C++中的三种基本循环结构:while循环、do...while循环和for循环,通过实例展示了它们的用法。此外,还提到了循环控制语句break和continue的作用,以及不推荐使用的goto语句。文章最后讨论了嵌套循环的应用,如打印九九乘法表。
3373

被折叠的 条评论
为什么被折叠?



