C++基础入门学习笔记之【程序流程结构】(三)

程序流程结构

c/c++支持最基本的3种程序运行结构,顺序结构,选择结构、循环结构

在这里插入图片描述

1、选择结构

1.1 单行if语句

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

在这里插入图片描述


在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
int main() {
	//用户输入分数,如果分数大于600则视为考上一本大学,否则没有考上一本大学
	int score = 0;
	cout << "请输入分数:" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	//注意:if后面不能加;号
	if (score > 600)
	{
		cout << "恭喜您考上一本大学!" << endl;
	}
	return 0;
}

1.2 多行if语句

在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
int main() {
	//用户输入分数,如果分数大于600则视为考上一本大学,否则输出没有考上一本大学
	int score = 0;
	cout << "请输入分数:" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	//if后面不能加;号
	if (score > 600)
	{
		cout << "恭喜您考上一本大学!" << endl;
	}
	else
	{
		cout<<"未考上一本大学"<<endl;
	}
	return 0;
}

1.3 多条件if语句

在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
int main() {
	//用户输入分数,如果分数大于600则视为考上一本大学,否则输出没有考上一本大学
	int score = 0;
	cout << "请输入分数:" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	//if后面不能加;号
	if (score > 600)
	{
		cout << "恭喜您考上一本大学!" << endl;
	}
	else if (score > 500)
	{
		cout << "恭喜您考上二本大学!" << endl;
	}
	else if (score > 400)
	{
		cout << "恭喜您考上三本大学!" << endl;
	}
	else
	{
		cout<<"未考上本科大学"<<endl;
	}
	return 0;
}

1.4 嵌套if语句
#include<iostream>
using namespace std;
#include<string>
int main() {
	//用户输入分数,如果分数大于600则视为考上一本大学,否则输出没有考上一本大学
	int score = 0;
	cout << "请输入分数:" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	//if后面不能加;号
	if (score > 600)
	{
		cout << "恭喜您考上一本大学!" << endl;
		if (score > 700)
		{
			cout << "恭喜您能考入北京大学!" << endl;
		}
		else if (score > 650)
		{
			cout << "恭喜您能考入清华大学!" << endl;
		}
		else 
		{
			cout << "恭喜您能考入人民大学!" << endl;
		}
	}
	else if (score > 500)
	{
		cout << "恭喜您考上二本大学!" << endl;
	}
	else if (score > 400)
	{
		cout << "恭喜您考上三本大学!" << endl;
	}
	else
	{
		cout<<"未考上本科大学"<<endl;
	}
	return 0;
}

1.5 综合案例

案例如下:
在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
int main() {
	//三只小猪称体重,判断哪只重
	int num1 = 0;
	int num2 = 0;
	int num3 = 0;
	//让用户输入三只小猪的重量
	cout << "请输入小猪A的体重" << endl;
	cin >> num1;
	cout << "请输入小猪B的体重" << endl;
	cin >> num2;
	cout << "请输入小猪C的体重" << endl;
	cin >> num3;
	//打印三只小猪的体重
	cout << "小猪A的体重为" << num1 << endl;
	cout << "小猪B的体重为" << num2 << endl;
	cout << "小猪C的体重为" << num3 << endl;
	//判断哪只小猪最重
	if (num1 > num2)
	{
		if (num1 > num3)
		{
			cout << "小猪A最重" << endl;
		}
		else
		{
			cout << "小猪C最重" << endl;
		}
	}
	else
	{
		if (num2 > num3)
		{
			cout << "小猪B最重" << endl;
		}
		else
		{
			cout << "小猪C最重" << endl;
		}
	}
	return 0;
}

1.6 三目运算符

作用:通过三目运算符实现简单的判断
注意:c++中三目运算符返回的是变量,可以继续赋值

在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
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;
	return 0;
}

输出:

c=20
a=10
b=100
c=20
1.7 switch语句

作用:执行多条件分支语句
switch和if语句的区别:
1、switch判断的条件只能是整型或字符型,不能是一个区间;
2、结构清晰,执行效率高;
注意:
case里面如果没有break,程序会一直向下执行;

语法:
在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
int main() {
	//提示用户给电影打分
	cout << "请给电影打分" << endl;
	//用户开始打分
	int score = 0;
	cin >> score;
	cout << "score=" << score << endl;
	//根据用户输入的分数提示用户输入的结果
	switch (score)
	{
	case 10:
		cout << "经典电影" << endl;
		break;
	case 9:
		cout << "good电影" << endl;
		break;
	default:
		cout << "一般电影" << endl;
		break;
	}
	return 0;
}

输出:

请给电影打分
9.5
score=9
good电影

2、循环结构

2.1 while结构

作用:满足循环条件,执行循环语句
语法:while(循环条件){循环语句}
解释:只要循环的条件为真就会执行循环语句

#include<iostream>
using namespace std;
#include<string>
int main() {
	//while循环,在屏幕中打印0-9数字
	int num = 0;
	while (num < 10) 
	{
		cout << "num="<<num << endl;
		num++;
	}
	return 0;
}

输出:

num=0
num=1
num=2
num=3
num=4
num=5
num=6
num=7
num=8
num=9
2.1.1 案例练习
#include<iostream>
using namespace std;
#include<string>
//time系统时间头文件包含
#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;
	//2、玩家猜测
	int val = 0;
	while (val != num)
	{
		cout << "玩家输入数字:" << endl;
		cin >> val;
		//3、游戏判断
		if (val > num)
		{
			cout << "输入大了" << endl;
		}
		else if (val < num)
		{
			cout << "输入小了" << endl;
		}
		else
		{
			cout << "输入正确" << num << endl;
		}
	}
	return 0;
}

输出:

22
玩家输入数字:
23
输入大了
玩家输入数字:
20
输入小了
玩家输入数字:
22
输入正确22
2.1.2 do…while循环语句

作用:满足循环条件,执行循环语句
语法:do{循环语句}while(循环条件)

与while的区别:

do…while会先执行循环语句,再判断循环条件,与while正好相反

#include<iostream>
using namespace std;
#include<string>
int main() {
	//do...while语句,在屏幕上打印0-9数字
	int num = 0;
	do 
	{
		cout << num << endl;
		num++;
	} while (num < 10);
	return 0;
}

输出:

0
1
2
3
4
5
6
7
8
9
2.1.3 案例练习

求解三位数字的水仙花数

#include<iostream>
using namespace std;
#include<string>
int main() {
	//求解水仙花数,即一个三位数的每一位的三次方加和仍等于他本身
	int num = 100;
	do {
		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);
	return 0;
}

输出:

2.2 for循环

作用:满足循环条件,执行循环语句
语法:for(起始表达式;条件表达式;末尾循环体){循环语句;}

执行顺序:
在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
int main() {
	//for循环
	for (int i=0; i < 10; i++) {
		cout << i << endl;
	}
	return 0;
}

输出:

0
1
2
3
4
5
6
7
8
9
2.2.1 for循环案例
#include<iostream>
using namespace std;
#include<string>
int main() {
	// 0-100个数,打印十位数、个位数含7或能被7整除的数,
	int num = 1;
	for (num;num < 101;num++) 
	{
		if (num % 10 == 7 || num % 7 == 0 || num / 10 == 7)
		{
			cout << "敲桌子" << num << endl;
		}
		else
		{
			cout << num << endl;
		}
	}
	return 0;
}

输出:

1
2
3
4
5
6
敲桌子7
8
9
10
11
12
13
敲桌子14
15
16
敲桌子17
18
19
20
敲桌子21
22
23
24
25
26
敲桌子27
敲桌子28
29
30
31
32
33
34
敲桌子35
36
敲桌子37
38
39
40
41
敲桌子42
43
44
45
46
敲桌子47
48
敲桌子49
50
51
52
53
54
55
敲桌子56
敲桌子57
58
59
60
61
62
敲桌子63
64
65
66
敲桌子67
68
69
敲桌子70
敲桌子71
敲桌子72
敲桌子73
敲桌子74
敲桌子75
敲桌子76
敲桌子77
敲桌子78
敲桌子79
80
81
82
83
敲桌子84
85
86
敲桌子87
88
89
90
敲桌子91
92
93
94
95
96
敲桌子97
敲桌子98
99
100
2.2 嵌套循环

作用:在循环体中再嵌套一层循环,解决一些实际问题

#include<iostream>
using namespace std;
#include<string>
int main() {
	//打印10*10的*图
	// 外层循环一次,内层循环一周
	//外层循环
	for (int i = 0; i < 10; i++)
	{ 
		//内层循环
		for (int j = 0; j < 10; j++)
		{
			cout << "* ";
		}
	cout << endl;
	}
	return 0;
}

输出:

* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *

2.2.1 案例:乘法口诀表
#include<iostream>
using namespace std;
#include<string>
int main() {
	for(int i=1;i<10;i++)
	{
		for (int j = 1; j <= i; j++)
		{
			cout <<j<<"*"<<i<<"="<< j * i << "  ";
		}
		cout << endl;
	}
	return 0;
}

输出:

1*1=1
1*2=2  2*2=4
1*3=3  2*3=6  3*3=9
1*4=4  2*4=8  3*4=12  4*4=16
1*5=5  2*5=10  3*5=15  4*5=20  5*5=25
1*6=6  2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
1*7=7  2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
1*8=8  2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
1*9=9  2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81

3、 跳转语句

用于跳出选择结构和循环结构

3.1 break语句

在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
int main() {
	//break的使用时机
	//1.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;
	}
	//2、循环语句
	for (int i = 0; i < 10; i++)
	{
		if (i > 5)
		{
			break;
		}
		
		cout << i << endl;
	}
	//3、嵌套循环语句
	for (int i = 0; i < 10; i++)
	{
		//内层循环
		for (int j = 0; j < 10; j++)
		{
			if(j==5)
			{
				break;
			}
			cout << "* ";
		}
		cout << endl;
	}

}

输出:

请输入副本难度
1、普通难度
2、中等难度
3、困难难度
1
您选择的是普通难度
0
1
2
3
4
5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
3.2 continue语句

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

#include<iostream>
using namespace std;
#include<string>
int main() {
	//打印0-100中的奇数
	for (int i=0;i<100;i++)
	{
		if (i % 2 == 0)
		{
			continue;
		}
		cout << i << endl;
	}
	return 0;
}

输出:

1 
3 
5 
7 
9 
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99
3.3 goto语句

作用:可以无条件跳转语句
语法:goto标记;
注意:标记名称一般用大写
解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置

在程序中不建议使用 goto 语句,以免造成程序混乱;

#include<iostream>
using namespace std;
#include<string>
int main() {
	//goto语句
	cout << "1....." << endl;
	cout << "2....." << endl;
	goto FLAG;
	cout << "3....." << endl;
	cout << "4....." << endl;
	FLAG:
	cout << "5....." << endl;
	system("pause");
	return 0;
}

输出:

1.....
2.....
5.....
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值