第二节 流控制
1. 关系运算符
符号 | 意义 | 运算结果 |
> | 大于 | 5 > 5 = false |
>= | 大于等于 | 5 >=5 = true |
< | 小于 | 5 < 5 = false |
<= | 小于等于 | 5 <= 5 = true |
== | 等于 | 5 == 5 = true |
!= | 不等于 | 5 != 5 = false |
注意:== 才是进行等于判断, = 是进行赋值操作,赋值操作的结果永远都是 true。
2. 逻辑运算符
符号 | 意义 | 运算结果 |
&& | 与 | T && T = T; T && F = F; F && F =F |
|| | 或 | T || T = T; T || F = F; F || F = F |
! | 非 | !T = F; !F = T |
注意: 在C++中,true和1(或非0)相同,false和0相同
3. 条件判断
条件判断,主要用来验证表达式的结果,条件判断的结构如下
if(conditon) { // todo } else if(condition) { // todo } else { // todo }
对于条件判断而言,只要列表中的条件有一个得到满足,其他的判断都不再执行
如果给出的所有条件都不满足,则执行else中的代码
#include <iostream> using namespace std; int main() { int x = 6; int y = 2; if(x > y) cout << "x is greater than y\n"; else if(y > x) cout << "y is greater than x\n"; else cout << "x and y are equal\n"; return 0; }
注意:if...else结构后面是没有分号的,如果if...else中的代码只有一行可以省略{ }
// 输入两个数字,输出较大的数字 #include <iostream> using namespace std; int main() { int a, b; cin >> a; cin >> b; if(a>b) cout << a; else cout << b; returen 0; }
// 输入一个ASCII字符,判断是字母、数字还是其他符号 #include <iostream> using namespace std; int main() { int character; cin >> character; if(character >= 48 && character <= 57) cout << "输入的是数字"; else if((character >= 65 && character <= 90) || (character >= 97 && character <= 122)) cout << "输入的是字母"; else cout << "输入的是其他符号"; returen 0;
if(5 > 0)
cout << "真" << endl;
else
cout << "假" << endl;
cout << (5 > 0) ? "真" : "假" << endl; }
4. 循环控制
当在满足一定条件下,需要重复执行操作时,就可以使用循环控制语句。
while(condition){ // todo } do { // todo } while(contidion)
#include <iostream> using namespace std; int main() { int x = 0; while(x < 10){ x = x + 1; cout << "x is " << x << "\n"; } return 0; }
for(initialization; condition; incrementation){ // todo }
#include <iostream> using namespace std; int main() { for(int x = 0; x < 10; x = x + 1) cout << x << "\n"; return 0; }
#include <iostream> using namespace std; int main() { int x = 0; // 注意此时for循环的初始化操作,被放到for循环的外面 for(; x < 10; x = x + 1) cout << x << "\n"; return 0; }
//for循环与while循环可以相互转化 #include <iostream> using namespace std; int main() { for(int x = 0; x < 10; x = x + 1) cout << x << "\n"; return 0; } #include <iostream> using namespace std; int main() { int x = 0; while(x < 10){ cout << x << "\n"; x = x + 1; } return 0; }
for(int i = 0; i < 10; i++) { if(i % 2 == 0) { continue; } cout << i << endl; } for(int i = 0; i < 10; i++) { if(i % 2 = 0) { break; } cout << i << endl; }
5. GOTO 跳转
不推荐使用
#include <iostream> using namespace std ; // 声明整形变量a,此时变量a还未被分配内存空间 //extern int a; int main () { // 两者区别在于,goto语句不会自动释放局部变量的内存空间 int a = 0; reset: a = 0; a = 10; cout << a << endl; goto reset; while(1) { int a = 0; a = 10; } return 0; }
6. continue, break, return, exit()
如何中断一个循环?
#include <iostream> using namespace std ; int main () { // continue 结束本次逻辑循环,继续下次逻辑循环 for(int a = 0; a < 10; a++) { if(a % 2 == 0) continue; cout << a << " " << endl; } cout << "END" << endl; // break 结束全部逻辑循环判断 for(int a = 0; a < 10; a++) { if(a % 2 == 0) break; cout << a << " " << endl; } cout << "END" << endl; // return 结束当前函数 for(int a = 0; a < 10; a++) { if(a % 2 == 0) return 0; cout << a << " " << endl; } cout << "END" << endl; // exit() 结束当前程序 for(int a = 0; a < 10; a++) { if(a % 2 == 0) exit(0); cout << a << " " << endl; } cout << "END" << endl; }
除了if...else if...else多条件判断,还可以使用switch....case结构进行多条件判断,区别是switch...case中的全部条件判断都会被执行
#include <iostream> using namespace std; int main() { int x = 6; switch(x){ case 1: cout << "x is 1\n"; break; case 2: case 3: cout << "x is 2 or 3"; break; default: cout << "x is not 1, 2, or 3"; } return 0; }
7. 练习
// 在屏幕中打印出从1-99中所有的偶数 #include <iostream> using namespace std; int main() { for(int x = 0; x < 99; x = x + 1) if(x%2 == 0) cout << x << endl; return 0; }
//利用do…while结构创建屏幕菜单,1. 打印姓名,2. 打印学号,3. 退出程序 #include <iostream> using namespace std; int main() { int userInput = 0; do { cout << "请输入选择菜单编号:" << endl; cout << "1. 打印姓名" << endl; cout << "2. 打印学号" << endl; cout << "3. 退出程序" << endl; cin >> userInput; switch(x){ case 1: cout << "庞兴庆"; break; case 2: cout << "学号:009126"; break; default: cout << "输入不是 1, 2, or 3"; }while(userInput != 3) } return 0; }
//使用双重循环在屏幕打印九九乘法表 #include <iostream> using namespace std; int main() { for(int i = 1; i < 10; i++) { for(int j = 1; j < 10; j++) { cout << i << " * " << j << " = " << i * j << " "; } cout << endl; } return 0; }
6. 作业
作业1:根据用输入的三个角度,判断这三个角度能够构成三角形
作业2:在屏幕中输出小于100的奇数
作业3:根据用户输入的数字,判断数字的类型(整数或小数)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析