02.选择结构与循环结构
- 一.程序流程结构
- 1.选择结构
- 1.1.if语句
- 1.2.三目运算符
- 1.3.switch语句
- 2.循环结构
- 2.1.while语句
- 2.2.do-while语句
- 2.3.for语句
- 2.4.break语句
- 2.5.continue语句
- 2.6.goto语句
一.程序流程结构
- C/C++支持的最基本的运行结构: 顺序结构, 选择结构, 循环结构
- 顺序结构:程序按顺序执行,不发生跳转
- 选择结构: 依据条件是否满足,有选择的执行相应的代码
- 循环结构: 依据条件是否满足, 循环多次自行代码
1.选择结构
1.1.if语句
#include <iostream>
using namespace std;int main(){// 单行格式的if语句: if(条件) { 语句 }int score = 0;cout << "请输入分数: " << endl;cin >> score;if (score >= 60) { cout << "及格!" << endl; }cout << "==============================" << endl;// if-else语句cout << "请输入分数: " << endl;cin >> score;if (score >= 60) {cout << "及格!" << endl;}else {cout << "不及格!" << endl;}// if-else-if语句cout << "请输入分数: " << endl;cin >> score;if (score >= 90) {cout << "优秀!" << endl;}else if (score >= 80) {cout << "良好!" << endl;}else if (score >= 60) {cout << "合格!" << endl;}else {cout << "不及格!" << endl;}system("pause");return 0;
}
案例: 三只小猪称重,要求依次输入三只小猪的体重,判断哪一只猪最重
#include <iostream>
using namespace std;int main() {int pig1, pig2, pig3 = 0;cout << "第一只猪有多重: " << endl;cin >> pig1;cout << "第二只猪有多重: " << endl;cin >> pig2;cout << "第三只猪有多重: " << endl;cin >> pig3;// 判断语句if (pig1 > pig2) {if (pig1 > pig3) {cout << "第一只小猪最重!" << endl;}else if (pig1 == pig3) {cout << "一,三小猪最重!他们一样重!" << endl;}else {cout << "第三只小猪最重!" << endl;}}else if (pig1 == pig2) {if (pig1 > pig3) {cout << "一,二小猪最重!他们一样重!" << endl;}else if (pig1 == pig3) {cout << "三只小猪一样重!" << endl;}else {cout << "第三只小猪最重!" << endl;}}else {if (pig2 > pig3) {cout << "第二只小猪最重!" << endl;}else if (pig2 == pig3) {cout << "二,三小猪最重!他们一样重!" << endl;}else {cout << "第三只小猪最重!" << endl;}}system("pause");return 0;
}
1.2.三目运算符
- 可以实现较为简单的条件判断
- 语法
表达式1 ? 表达式2 : 表达式3
如果表达式1
为真,那执行表达式2
并返回结果,反之执行表达式3
并返回结果 C++中三目运算符返回的是变量本身,是可以继续赋值的
int a = 10;
int b = 20;
int c = 0;c = (a > b ? a : b); // c=b
(a > b ? a : b) == 100; // b=100
1.3.switch语句
- switch语句中的条件可以为,整型,字符型,枚举型或class类型(class中有单一的函数将其转换为整型)
switch(表达式) {case 结果1:执行语句;break;...default:执行语句;break;}
2.循环结构
2.1.while语句
- 满足循环条件就一直循环, 先判断条件再执行循环语句
- 语法:
while(循环条件){ 循环语句; }
#include <iostream>
using namespace std;int main() {int num = 0;// 输出0~9while (num < 10) {num++;cout << num << endl;}system("pause");return 0;
}
2.2.do-while语句
- 执行语句,如果满足循环条件的话
- 与while的区别在于会先执行一次do当中的内容,再开始循环
- 语法
do { 循环语句; } while( 循环条件 );
// 输出0-9
int num = 0;
do {cout << num << endl;num++;
} while (num < 10);
案例寻找水仙花数:
#include <iostream>
using namespace std;int main() {
// 水仙花数: 三位数,每一位的三次幂等于它本身int num = 100;do {int bai = num / 100; // 拿到百位int shi = num / 10 % 10; // 拿到十位int ge = num % 10; //拿到个位int result = (int)pow(bai, 3) + (int)pow(shi, 3) + (int)pow(ge, 3);if (result == num) { // 求和验证判断输出cout << "find: " << num << endl;}num++;} while (num < 1000);system("pause");return 0;
}
2.3.for语句
- 设定初值, 满足条件, 执行循环语句
- 语法:
for(起始表达式; 条件表达式; 末尾循环体) { 循环语句 ; }
for语句中的所有条件都可以省略
// 用for循环输出0-9, 相比while语句简洁很多
for(int i=0;i < 10;i++) {cout << i << endl;
}
案例练习:敲桌子,1-100的数,如果含有7或者是7的倍数打印‘敲桌子’其余数字直接输出。
#include <iostream>
using namespace std;int main() {for (int i = 1; i <= 100; i++) {int ge = i % 10;int shi = i / 10 % 10;if (i % 7 == 0 || ge == 7 || shi == 7) {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 < 10; j++) { // 每一个数都两两相乘cout << j << "*" << i << "=" << (i * j) << "\t";}cout << "\n" << endl; // 一个数结束之后换行输出}cout << "-------------------------------" << endl;// 阶梯形式for (int i = 1; i < 10; i++) {for (int j = 1; j <= i; j++) { // 只从1乘到当前的数,做到了矩阵输出cout << j << "*" << i << "=" << (i * j) << "\t";}cout << "\n" << endl;}system("pause");return 0;
}
2.4.break语句
- 作用:跳出选择结构或者循环结构
- 使用时机
在switch-case
结构中终止当前case并跳出switch
在循环语句中跳出当前的循环语句(多层循环跳出最近的内层循环)
打印阶梯乘法表的时候,也能通过break语句实现
// 阶梯形式for (int i = 1; i < 10; i++) {for (int j = 1; j < 10; j++) { // 只从1乘到当前的数,做到了矩阵输出cout << j << "*" << i << "=" << (i * j) << "\t";if (j >= i) break;}cout << "\n" << endl;}
2.5.continue语句
- 在循环中跳过, 跳过本次循环剩余的语句,直接执行下一轮循环
// 输出1-100中所有的奇数
for(int i = 1;i <= 100;i++) {if(i % 2 == 0) continue;cout << i << endl;
}
2.6.goto语句
- 语法: ···goto 标记;```
- 如果标记的名称存在那么执行到沟通语句时会跳转到标记的位置
// 如下语句会直接跳过字符2的打印
cout << "1" << ENDL;
goto FLAG;
cout << "2" << endl;
FLAG: // 注意这里是冒号
cout << "3" << endl;
学习笔记与课程计划
B站视频链接