C++程序流程结构——选择结构
C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
- 顺序结构:程序按顺序执行,不发生跳转
- 选择结构:一句条件是否满足,有选择的执行相应功能
- 循环结构:一句条件是否满足,循环多次执行某段代码
接下来先介绍选择结构,之后单独发博客介绍循环结构
一、if 语句
作用:执行满足条件的语句
if语句的三种形式
- 单行格式 if 语句
- 多行格式 if 语句
- 多条件的 if 语句
1、单行格式 if 语句
if (条件)
{
条件满足执行的语句;
}
2、多行格式 if 语句
if (条件)
{
条件满足执行的语句;
}
else
{
条件不满足执行语句;
}
下面通过一道例题进行实践
#include <iostream>
using namespace std;
/**
* 用户输入分数,如果分数大于600,表示考上一本大学,在屏幕输出
* 1、用户输入分数
* 2、打印用户输入的分数
* 3、判断分数是否大于600,如果大于,那么输出
*/
int main() {
int score = 0;
cout << "Enter your score : " << endl;
cin >> score;
cout << "Your score is : " << score << endl;
if (score > 600) {
cout << "Your score is above 600, you can go to a university." << endl;
} else {
cout << "Your score is less than 600, you can not go to another university." << endl;
}
return 0;
}
大于600的情况
小于600的情况
3、多条件的 if 语句
if (条件1)
{
条件1满足执行的语句;
}
else if (条件2)
{
条件2满足执行的语句;
}
else
{
两个条件都不满足执行的语句;
}
例子实践
#include <iostream>
using namespace std;
/**
* 选择结构,多条件if语句
* 输入一个考试分数。如果大于600分,视为考上一本大学,在屏幕输出
* 大于500,是为考上二本大学,屏幕输出
* 大于400,视为考上三本大学,屏幕输出
* 小于等于400,视为未考上本科,屏幕上输出
*/
int main() {
int score = 0;
cout << "Enter your score: ";
cin >> score;
cout << "Your score is: " << score << endl;
if (score > 600) {
cout << "Successfully admitted to a college" << endl;
} else if (score <= 600 && score > 500) {
cout << "Successfully admitted to two colleges" << endl;
} else if (score <= 500 && score > 400) {
cout << "Successfully admitted to three universities" << endl;
} else {
cout << "Successfully roasted sweet potato.【手动狗头】" << endl;
}
return 0;
}
4、嵌套 if 语句
在 if 语句中,可以嵌套使用 if 语句,达到更精确的条件判断
例子实践一
感jio用英语会比较洋气(bushi)
#include <iostream>
using namespace std;
/**
* 案例需求:
* 提示用户输入一个高考考试分数,根据分数做如下判断:
* 分数如果大于600分视为考上一本,大于500分考上二本,大于400考上三本,其余视为未考上本科;
* 在一本分数中,如果大于700分,考上北大,大于650分,考入清华,大于600考入人大。
*/
int main() {
int score = 0;
cout << "Enter your score : " << endl;
cin >> score;
cout << "Your score is : " << score << endl;
if (score > 600) {
//一本
if (score > 700) {
//北大
cout << "Congratulations on your admission to Peking University" << endl;
}
else if (score <= 700 && score > 650) {
//清华
cout << "Congratulations on your admission to Tsinghua University" << endl;
}
else {
//人大
cout << "Congratulations on your admission to Renmin University" << endl;
}
}
else if (score <= 600 && score > 500) {
//二本
cout << "Congratulations, you have been admitted to the second college" << endl;
}
else if (score <= 500 && score > 400) {
//三本
cout << "Congratulations, you have been admitted to three colleges" << endl;
}
else {
//未考上
cout << "I'm sorry. Give it another year" << endl;
}
return 0;
}
例子实践二
感jio用英语会比较洋气(bushi)
#include <iostream>
using namespace std;
/**
* 练习案例:三只小猪称体重
* 有三支小猪ABC,请分别输入三只小猪的体重,并判断那只小猪最重
*/
int main() {
// 小猪 A B C
float pig_A = 0.0f;
float pig_B = 0.0f;
float pig_C = 0.0f;
cout << "Please enter the weight of piglet A:";
cin >> pig_A;
cout << "Please enter the weight of piglet B:";
cin >> pig_B;
cout << "Please enter the weight of piglet C:";
cin >> pig_C;
if (pig_A > pig_B) {// 如果小猪 A 的体重比 小猪 B 的体重重
if (pig_A > pig_C) {// 如果小猪 A 的体重比 小猪 C 的体重重
cout << "Little pig A is the heaviest" << endl;
} else {// 如果小猪 A 的体重比 小猪 C 的体重轻
cout << "Little pig C is the heaviest" << endl;
}
} else {
if (pig_B > pig_C) {
cout << "Little pig B is the heaviest" << endl;
} else {
cout << "Little pig C is the heaviest" << endl;
}
}
return 0;
}
二、三目运算符
作用:通过三目运算符实现简单的判断
语法:用于接收返回的字面量 = 表达式1 ? 表达式2 : 表达式3
解释:
如果表达式1的值为真,执行表达式2,并返回表达式2的结果;
如果表达式1的值为假,执行表达式3,并返回表达式3的结果。
代码
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
int c = 30;
int d = 40;
int e = 0;
// 进行三目运算
e = a > b ? c : d;
// 猜一下结果是多少,复制到你的编译器看看结果对不对呢
// 并且在C++中,三木运算符返回的是变量,可以继续赋值
(a > b ? c : d) = 100;
cout << "a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << ", e = " << e << endl;
return 0;
}
三、switch语句
作用:执行多条件分支语句
语法:
switch (表达式)
{
case 结果1:执行语句;break
case 结果2:执行语句;break
……
default:执行语句;break;
}
代码
#include <iostream>
using namespace std;
int main() {
int num = 0;
cout << "输入一个数字「0~3」表示接下来的操作" << endl;
cin >> num;
cout << "您的选择是 : " << num << " 号" << endl;
/**
* if 和 switch 的区别:
* switch 缺点:判断的时候只能是整形或者字符型,不可以是条件(区间)
* switch 优点:结构清晰,执行效率高
*/
switch (num) {
case 0:
cout << "零零碎碎" << endl;
break;
case 1:
cout << "一枝独秀" << endl;
break;
case 2:
cout << "二二二二" << endl;
break;
case 3:
cout << "三羊开泰" << endl;
break;
default:
cout << "啥也不是" << endl;
break;
}
return 0;
}