[C++基础学习]----03-程序流程结构之循环结构详解

发布于:2024-04-27 ⋅ 阅读:(30) ⋅ 点赞:(0)

前言

        在C++程序中,循环结构在用于重复执行一段代码块,直到满足某个条件为止。循环结构有多种形式,包括while循环、do-while循环和for循环。

正文

01-循环结构简介

       1、while循环语句:

        while循环在每次循环开始前检查条件是否为真,如果真则执行循环体中的代码块,然后继续检查条件。

        语法格式为:

while (condition)
{
    // code to be executed while condition is true
}

        2、do-while循环语句:

        do-while循环先执行一次循环体中的代码块,然后在每次循环结束后检查条件是否为真,如果真则继续执行。

        语法格式为:

do
{
    // code to be executed
} while (condition);

        3、for循环语句:

        for循环是一种计数循环,通过初始化、条件判断和更新计数器来控制循环的执行次数。

        语法格式为:

for (initialization; condition; update)
{
    // code to be executed for each iteration
}

02-while循环

        具体使用代码和解释如下:

#include<iostream>
using namespace std;

int main()
{
	//循环结构 while
	//输出0~9十个数
	int num = 0;
	//while后的括号中一定要写循环条件,还有括号之后不能加分号
	while (num < 10)
	{
		//num++;在这个位置输出1~10
		cout << num << endl;
		num++;//在这个位置输出0~9
	}

	system("pause");

	return 0;

}

03-案例-猜数字游戏

        具体使用代码和解释如下:

#include<iostream>
using namespace std;
#include <ctime>
int main()
{
	// 若想每次生成的随机数都不相同,需要加入一个随机数种子,利用系统时间生成随机数

	srand((unsigned int)time(NULL));

	//1、首先系统生成一个随机数
	int num = rand() % 100 + 1;     // rand()%100这样生成一个0-99的随机数  rand()%100+1生成1-100的随机数
//	cout << num << endl;   // 随机生成了一个数,范围在1-100
	//2、玩家进行猜测
	int val = 0;  //玩家输入数据
	int num1 = 0;
	while (num1<5)
	{
		cin >> val;
		cout << "玩家输入的值为:" << val << endl;
		//3、判断玩家的猜测

		// 猜测错误   提示猜的结果,过大或者过小 重新返回第二步
		if (val > num)
		{
			cout << "猜测过大" << endl;
		}
		else if (val < num)
		{
			cout << "猜测过小" << endl;
		}
		else
		{
			cout << "恭喜您猜测正确" << endl;

			break; // 可以使用该关键字退出循环
		}
		num1++;
		
	}
	cout << "不好意思,挑战失败" << endl;
	 // 猜测正确   退出游戏
	 // 猜测错误   提示猜的结果,过大或者过小 重新返回第二步


	system("pause");

	return 0;  

}

04-do-while语句

        具体使用代码和解释如下:

#include<iostream>
using namespace std;

int main()
{
	int num = 0;
	//do
	//{
	//	cout << num << endl;
	//	num++;
	//}
	//while (num < 10);//do while 与while 的区别:前者先执行一次输出,在进入循环。后者不会先执行

	while (num<10)//这个会先判断循环条件,再进行输出。
	{
		cout << num << endl;
		num++;
	}
	system("pause");

	return 0;
}

05-案例-水仙花数

        具体使用代码和解释如下:

#include<iostream>
using namespace std;

int main()
{
	//1、先打印所有的三位数字
	int num = 100;
	
	do
	{
		//2、从所有三位数中找到水仙花数
		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 < 1000);//只打印三位数


// 	int num = 0;
// 	while (num<100)
// 	{
// 		cout << num << endl;
// 		num++;
// 	}


	system("pause");
	return 0;

}

06-for循环

        具体使用代码和解释如下:

#include<iostream>
using namespace std;

int main()
{
	//for循环
	//打印数字0~9
	// for的括号里包含了三种句子,起始表达式;条件表达式;末尾循环体
	for (int i = 0; i < 10; i++)//i++可以写到循环体里面,也可以写到括号里
		//这就是for循环更加精辟的地方,代码行数更少
	{
		cout << i << endl;
	}//此程序先执行int i=0;然后先判断i<10,之后输出0,接着执行i++,持续循环

	system("pause");
	return 0;

}

07-案例-敲桌子

        具体使用代码和解释如下:

#include<iostream>
using namespace std;

int main()
{
	//1、先输出1~100个数字
	for (int i= 1; i <101; i++)
	{
		//2、找到特殊的数字,改为输出敲桌子,7的倍数,十位有7,个位有7
		if (i % 7 == 0 || i % 10 == 7||i/10==7)//特殊数字,输出敲桌子
		{
			cout << "敲桌子" << endl;
		}
		else //不是特殊数字,直接输出
		{
			cout << i << endl;
		}
	}

	system("pause");
	return 0 ;

}

08-嵌套循环

        具体使用代码和解释以及运行结果如下:

#include<iostream>
using namespace std;

int main()
{
	//嵌套循环,输出一个星图
	//打印一行星图
	//外层循环一次,内层循环一周
	for (int i = 0; i < 10; i++)//外层循环
	/*int i = 0;
	while(i<10)*/
	{
		for (int j = 0; j < 10; j++)//内层循环
		{
			cout << "* "; //<< endl;这样直接在后面书写endl;,打印出的是一条竖线
		}
		/*i++;*/
		cout << endl;//当endl,这样写的时候,可以打印出横向的
	}
	system("pause");
	return 0;
}

09-案例-打印99乘法表

        具体使用代码和解释以及运行结果如下:

#include<iostream>
using namespace std;

int main()
{
	//乘法口诀表
	//打印行数
	for (int i = 1; i <= 9; i++)
	{
		//cout << i << endl;
		for (int j = 1; j <= i; j++)//行数小于等于列数
		{
			cout <<j<<"*"<<i<<"= "<<j*i<<"\t";//输出j(数)*i(数)=j*i,\t用来对齐
		}
		cout<< endl;//cout<<endl,写在这里,是为了不让上一步执行换行操作
	}

	system("pause");
	return 0;
}

总结

        循环结构允许程序重复执行特定的代码块,能够简化代码、提高效率并实现复杂的算法。在实际编程中,循环结构是经常使用的重要部分,能够有效地处理重复性任务和数据处理。


网站公告

今日签到

点亮在社区的每一天
去签到