4 程序流程结构
时间:2023-9-16 23:35 作者:wen 分类: C++
[toc]
程序流程结构
C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
- 顺序结构:程序按顺序执行,不发生跳转。
- 选择结构:依据条件是否满足,有选择的执行相应的功能。
- 循环结构:依据条件是否满足,循环多次执行某段代码。
顺序结构
作用:代码按顺序执行
#include <iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
system("pause");
return 0;
}
选择结构
作用:执行满足条件的语句。
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 (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 (score > 600) { cout << "恭喜您考上一本大学" << endl; } else { cout << "可惜没有考上一本大学" << endl; } system("pause"); return 0; }
-
多条件的if语句:`if(条件){条件满足执行的语句}else if(条件){条件满足执行的语句}else{条件不满足执行语句}``
#include <iostream> using namespace std; int main() { // 选择结构 单行if语句 // 用户输入增速,如果分数大于600,视为考上一本大学,在屏幕上显示。 int score = 0; cout << "请输入一个分数:" << endl; cin >> score; cout << "您输入的分数为:" << score << endl; 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() { int score = 0; cout << "请输入一个分数:" << endl; cin >> score; cout << "您输入的分数为:" << score << endl; if (score > 600) { 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; } system("pause"); return 0; }
练习案例:三只小猪称体重
有三只小猪abc,请分别输入三只小猪的体重,并且判断哪只小猪最重?
#include <iostream>
using namespace std;
int main()
{
float a = 0, b = 0, c = 0;
cout << "请输入A小猪的体重" << endl;
cin >> a;
cout << "请输入B小猪的体重" << endl;
cin >> b;
cout << "请输入C小猪的体重" << endl;
cin >> c;
cout << "您输入的A小猪的体重为:" << a << endl;
cout << "您输入的B小猪的体重为:" << b << endl;
cout << "您输入的C小猪的体重为:" << c << endl;
if (a > b) {
if (a > c) {
cout << "小猪A最重" << endl;
}
else {
cout << "小猪C最重" << endl;
}
}
else {
if (b > c) {
cout << "小猪B最重" << endl;
}
else {
cout << "小猪C最重" << endl;
}
}
system("pause");
return 0;
}
三目运算符
作用:通过三目运算符实现简单的判断。
语法:表达式1 ? 表达式2 : 表达式3
解释:
如果表达式1的值为真,执行表达式,2,并且返回表达式2的结果。
如果表达式1的值为假,执行表达式,3,并且返回表达式3的结果。
示例:
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 20, c = 0;
c = a > b ? a : b;
cout << "c = " << c << endl;
(a > b ? a : b) = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
switch语句
作用:执行多条分支语句
语法:
switch(表达式)
{
case 结果1: 执行语句;break;
case 结果2: 执行语句;break;
...
default:执行语句;break;
}
示例:
#include <iostream>
using namespace std;
int main()
{
int sorce = 0;
cout << "请给《西虹市首富》打分1-5分" << endl;
cin >> sorce;
switch (sorce)
{
case 5:
cout << "您认为电影非常好" << endl;
break;
case 4:
cout << "您认为电影比较好" << endl;
break;
case 3:
cout << "您认为电影一般" << endl;
break;
case 2:
cout << "您认为电影比较差" << endl;
break;
case 1:
cout << "您认为电影是烂片" << endl;
break;
default:
cout << "输入的分数不对" << endl;
break;
}
system("pause");
return 0;
}
循环结构
while循环语句
作用:满足循环条件,执行循环语句
语法:while(循环条件){循环语句}
解释:只要循环条件的结果为真,就执行循环语句
#include <iostream>
using namespace std;
int main()
{
int score = 10;
while (score > 0)
{
cout << score << endl;
score--;
}
system("pause");
return 0;
}
练习案例:猜数字
案例描述:系统随机生成一个1~100之间的数字。玩家进行猜测,如果猜错提示玩家数字过大,或过小,如果猜对,恭喜玩家胜利并且退出游戏。
#include <iostream>
#include <random>
using namespace std;
int main()
{
// 创建一个随机数生成器对象
random_device rd; // 用于获取真正的随机种子
mt19937 gen(rd()); // 使用Mersenne Twister 19937随机数引擎
// 定义生成随机数的范围
int min_number = 1;
int max_number = 100;
// 创建一个分布对象,指定生成整数的范围
uniform_int_distribution<int> distribution(min_number, max_number);
// 生成随机数
int random_number = distribution(gen);
/*
// 随机数简单写法
srand(unsigned(time(0)));
int random_number = rand() % 100 + 1;
*/
int score = 0;
while (true)
{
cout << "请输入一个整数:" << endl;
cin >> score;
if (score == random_number) {
cout << "恭喜,回答正确" << endl;
break;
}
else if(score > random_number) {
cout << "您输入的数值大了" << endl;
}
else {
cout << "您输入的数值小了" << endl;
}
}
system("pause");
return 0;
}
do...while循环语句
作用:满足循环条件,执行循环语句
语法:do{循环语句}while(循环条件);
注意:与while的区别在于do...while会先执行一次循环语句,再判断循环条件
#include <iostream>
using namespace std;
int main()
{
int num = 0;
do
{
cout << num << endl;
num++;
} while (num < 10);
system("pause");
return 0;
}
练习案例:水仙花数
案例描述:水仙花数是指一个三位数。他的每个位上的数字的3次幂之和等于它本身。
例如:1^3 + 5^3 + 3^3 = 153
请利用do...while语句,求出所有3位数中的水仙花数
#include <iostream>
using namespace std;
int main()
{
int num = 100;
do
{
int a = num / 100;
int b = num / 10 % 10;
int c = num % 10;
int sum = pow(a,3) + pow(b,3) + pow(c,3);
if (num == sum) {
cout << num << endl;
}
num++;
} while (num < 1000);
system("pause");
return 0;
}
153
370
371
407
for循环语句
作用:满足循环条件,执行循环语句
语法:for(起始条件;条件表达式;末尾循环体){循环语句;}
示例:
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
system("pause");
return 0;
}
练习案例:敲桌子
案例描述:从1开始数到数字100。如果数字个位含有7或者数字十位含有7。或者该数字是7的倍数,我们打印敲桌子。其余数字直接打印输出。
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <=100; i++)
{
int a = i / 10;
int b = i % 10;
int c = i % 7;
if (a == 7 || b == 7 || c == 0 ) {
cout << "敲桌子" << endl;
}
else {
cout << i << endl;
}
}
system("pause");
return 0;
}
嵌套循环
作用:在循环中再嵌套一层循环,解决一些实际问题
练习案例:乘法口诀表
案例描述:利用嵌套循环,实现九九乘法表
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <10; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j <<" * " << i << " = " << i * j << "\t";
}
cout << endl;
}
system("pause");
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
跳转语句
break语句
作用:用于跳出选择结构或者循环结构
break使用的时机:
- 出现再switch条件语句中,作用是终止case并跳出switch
- 出现在循环语句中,作用是跳出当前的循环语句
- 出现在嵌套循环中,跳出最近的内层循环语句
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
if (i == 5) {
break;
}
cout << i << endl;
}
system("pause");
return 0;
}
continue语句
作用:在循环语句中,跳过本次循环,继续执行下一次循环
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0) {
continue;
}
cout << i << endl;
}
system("pause");
return 0;
}
goto语句
作用:可以无条件跳转语句
语法:goto 标记;
解释:如果标记的名称存在,执行到goto语句是,会跳转到标记的位置
示例:
#include <iostream>
using namespace std;
int main()
{
cout << "1" << endl;
goto FLAG;
cout << "2" << endl;
cout << "3" << endl;
cout << "4" << endl;
FLAG:
cout << "5" << endl;
system("pause");
return 0;
}
标签: C/C++