C++入门03——程序流程结构

C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构

  • 顺序结构:程序按顺序执行,不发生跳转

  • 选择结构:依据条件是否满足,有选择的执行相应功能

  • 循环结构:依据条件是否满足,循环多次执行某段代码

1.选择结构

1.1 if语句

作用:执行满足条件的语句

if语句的三种形式

  • 单行格式if语句

  • 多行格式if语句

  • 多条件的if语句

单行格式if语句:

if(条件){ 条件满足执行的语句 } 

#include <iostream>
using namespace std;

int main() {

	//选择结构-单行if语句
	//输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印

	int score = 0;
	cout << "请输入一个分数:" << endl;
	cin >> score;

	cout << "您刚才输入的分数为:" << score << endl;

	// if 语句
	// 注意事项,在if判断语句后面,不要加分号
	if (score > 600)
	{
		cout << "可以考上大学!!" << endl;
	}

	system("pause");

	return 0;

 多行格式if语句:

if(条件){ 条件满足执行的语句 }else{ 条件不满足执行的语句 };

#include <iostream>
using namespace std;

int main() {

	//选择结构-单行if语句
	//输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印

	int score = 0;
	cout << "请输入一个分数:" << endl;
	cin >> score;

	cout << "您刚才输入的分数为:" << score << endl;

	// if 语句
	// 注意事项,在if判断语句后面,不要加分号
	if (score > 600)
	{
		cout << "可以考上大学!!" << endl;
	}
	else
	{
		cout << "考不上,回去种地吧!" << endl;
	}

	system("pause");

	return 0;

}

 注意:if条件表达式后不要加分号

 多条件的if语句:

if(条件1){ 条件1满足执行的语句 }else if(条件2){条件2满足执行的语句}... else{ 都不满足执行的语句}

 实例:

	int main() {

	int score = 0;

	cout << "请输入考试分数:" << endl;

	cin >> score;

	if (score > 600)
	{
		cout << "我考上了一本大学" << endl;
	}
	else if (score > 500)
	{
		cout << "我考上了二本大学" << endl;
	}
	else if (score > 400)
	{
		cout << "我考上了三本大学" << endl;
	}
	else
	{
		cout << "我未考上本科" << endl;
	}

	system("pause");

	return 0;
}

 嵌套if语句

在if语句中,可以嵌套使用if语句,达到更精确的条件判断。

案例需求:

  • 提示用户输入一个高考考试分数,根据分数做如下判断

  • 分数如果大于600分视为考上一本,大于500分考上二本,大于400考上三本,其余视为未考上本科;

  • 在一本分数中,如果大于700分,考入北大,大于650分,考入清华,大于600考入人大。

示例:

#include <iostream>
using namespace std;

int main() {

	//选择结构-单行if语句
	//输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印

	int score = 0;
	cout << "欢迎进入高考志愿填报系统,请输入您的高考分数:" << endl;
	cin >> score;

	cout << "您刚才输入的分数为:" << score << endl;

	// if 语句
	// 注意事项,在if判断语句后面,不要加分号
	if (score >= 600)
	{
		cout << "过一本线了!!" << endl;
		if (score > 700) {
			cout << "去北大吧,都超700了!!" << endl;
		}
		else if (score > 650) {
			cout << "去清华吧!大佬。" << endl;
		}

		else {
			cout << "去人大也还不错!" << endl;
		}
	}
	else if (500 < score )
	{
		cout << "考上二本,有书读了!!" << endl;
	}
	else if (400 < score) {
		cout << "只能上个三本了。" << endl;
	}
	else {
		cout << "补习吧" << endl;
	}

	system("pause");

	return 0;

}

 

1.2 三目运算符

作用: 通过三目运算符实现简单的判断

语法:表达式1 ? 表达式2 :表达式3

解释:

如果表达式1的值为真,执行表达式2,并返回表达式2的结果;

如果表达式1的值为假,执行表达式3,并返回表达式3的结果。

示例:

int main() {
​
    int a = 10;
    int b = 20;
    int c = 0;
​
    c = a > b ? a : b;
    cout << "c = " << c << endl;
​
    //C++中三目运算符返回的是变量,可以继续赋值
​
    (a > b ? a : b) = 100;
​
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << c << endl;
​
    system("pause");
​
    return 0;
}

总结:和if语句比较,三目运算符优点是短小整洁,缺点是如果用嵌套,结构不清晰

1.3 switch语句

作用:执行多条件分支语句

语法:

switch(表达式)

{

	case 结果1:执行语句;break;

	case 结果2:执行语句;break;

	...

	default:执行语句;break;

}

实例:

int main() {

	//请给电影评分 
	//10 ~ 9   经典   
	// 8 ~ 7   非常好
	// 6 ~ 5   一般
	// 5分以下 烂片

	int score = 0;
	cout << "请给电影打分" << endl;
	cin >> score;

	switch (score)
	{
	case 10:
	case 9:
		cout << "经典" << endl;
		break;
	case 8:
		cout << "非常好" << endl;
		break;
	case 7:
	case 6:
		cout << "一般" << endl;
		break;
	default:
		cout << "烂片" << endl;
		break;
	}

	system("pause");

	return 0;
}

注意1:switch语句中表达式类型只能是整型或者字符型

注意2:case里如果没有break,那么程序会一直向下执行

总结:与if语句比,对于多条件判断时,switch的结构清晰,执行效率高,缺点是switch不可以判断区间

2.循环结构

2.1 while循环语句

 

#include <iostream>
#include <random>
using namespace std;

int main() {
	 
	random_device rd; // 创建一个随机数生成器引擎
	mt19937 gen(rd()); // 使用伪随机数生成器

	uniform_int_distribution<int> distribution(1, 100); // 创建一个分布,指定随机数范围

	int random_number = distribution(gen); // 生成随机数
	int guess;
	int attempts = 0;

	cout << "欢迎来到猜数游戏!" << endl;
	
	while (true)
	{
		cout << "请猜1~100之间的数字: " << endl;
		cin >> guess;
		attempts++;

		if (guess < 1 || guess > 100) {
			cout << "猜的数字不在1-100的范围内,请重试!" << endl;
		}
		else if (guess < random_number) {
			cout << "你猜小了,再猜一猜!" << endl;
		}
		else if (guess > random_number) {
			cout << "你猜大了,再猜一猜!" << endl;
		}
		else {
			cout << "恭喜你猜对了,答案是:" << random_number << "。你猜了"<< attempts<<"次!!" << endl;
			break;
		}
	}

	system("pause");

	return 0;

}

2.2 do...while循环语句

 实例:

int main() {

	int num = 0;

	do
	{
		cout << num << endl;
		num++;

	} while (num < 10);
	
	
	system("pause");

	return 0;
}

总结:与while循环区别在于,do...while先执行一次循环语句,再判断循环条件 。

例子:计算1到10的累加:

#include <iostream>
using namespace std;

int main(){
	
	int sum = 0, i = 1;
	do {
		sum = sum + i;
		i++;
	} while (i <= 10);
	cout << "数字1-10的和为:" << sum << endl;

	
	system("pause");
	return 0;

}

练习案例:水仙花数

案例描述:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身

例如:1^3 + 5^3+ 3^3 = 153

请利用do...while语句,求出所有3位数中的水仙花数

 答案1:

#include <iostream>
using namespace std;

int main() {
	int i = 100, ge = 0, shi = 0, bai = 0;
	do {
		ge = i % 10;
		bai = i / 100;
		shi = (i - bai * 100) / 10;
		if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) {
			cout << "水仙花数:" << i << endl;
		}

		i++;
	} while (i < 1000);
	system("pause");
	return 0;

}

 

 答案2:

#include <iostream>
using namespace std;

int main() {
    int number = 100; // 从100开始查找三位数
    int sum, temp, remainder;

    cout << "水仙花数:";

    do {
        sum = 0;
        temp = number;

        while (temp > 0) {
            remainder = temp % 10; //依次拿到个位,十位,百位
            sum += (remainder * remainder * remainder); 
            temp /= 10;  // temp = temp /10
        }

        if (sum == number) {
            cout << number << " ";
        }

        number++;
    } while (number < 1000);

    cout << endl;

    return 0;
}

2.3 for循环语句

作用: 满足循环条件,执行循环语句

语法:for(起始表达式;条件表达式;末尾循环体) { 循环语句; }

 示例:

int main() {

	for (int i = 0; i < 10; i++)
	{
		cout << i << endl;
	}
	
	system("pause");

	return 0;
}

注意:for循环中的表达式,要用分号进行分隔

总结:while , do...while, for都是开发中常用的循环语句,for循环结构比较清晰,比较常用

3 跳转语句

3.1 break语句

作用: 用于跳出选择结构或者循环结构

break使用的时机:

  • 出现在switch条件语句中,作用是终止case并跳出switch

  • 出现在循环语句中,作用是跳出当前的循环语句

  • 出现在嵌套循环中,跳出最近的内层循环语句

 示例1:

	//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;
}

实例2:

int main() {
	//2、在循环语句中用break
	for (int i = 0; i < 10; i++)
	{
		if (i == 5)
		{
			break; //跳出循环语句
		}
		cout << i << endl;
	}

	system("pause");

	return 0;
}

示例3:

int main() {
	//在嵌套循环语句中使用break,退出内层循环
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (j == 5)
			{
				break;
			}
			cout << "*" << " ";
		}
		cout << endl;
	}
	
	system("pause");

	return 0;
}

3.2 continue语句

 作用:在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环

 

 示例:

int main() {

	for (int i = 0; i < 100; i++)
	{

        // 如果是奇数则输出,偶数则不输出
		if (i % 2 == 0)
		{
			continue;  // 到这就不会再向下执行,以后程序会开始下一次循环
		}
		cout << i << endl;
	}
	
	system("pause");

	return 0;
}

注意:continue并没有使整个循环终止,而break会跳出循环  

3.3 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;
}

输出:1 5  

注意:在程序中不建议使用goto语句,以免造成程序流程混乱  

猜你喜欢

转载自blog.csdn.net/m0_55196097/article/details/134035958
今日推荐