当前位置: 首页 > news >正文

C++中的运算符总结(4):逻辑运算符(下)

C++中的运算符总结(4):逻辑运算符(下)

以下程序演示了如何使用条件语句和逻辑运算符根据变量的值执行不同的代码行:

#include <iostream>
using namespace std;int main()
{cout << "Use boolean values(0 / 1) to answer the questions" << endl;cout << "Is it raining? ";bool isRaining = false;cin >> isRaining;cout << "Do you have buses on the streets? ";bool busesPly = false;cin >> busesPly;// Conditional statement uses logical AND and NOTif (isRaining && !busesPly)cout << "You cannot go to work" << endl;elsecout << "You can go to work" << endl;if (isRaining && busesPly)cout << "Take an umbrella" << endl;if ((!isRaining) && busesPly)cout << "Enjoy the sun and have a nice day" << endl;return 0;
}

输出:

Use boolean values(0 / 1) to answer the questions
Is it raining? 1
Do you have buses on the streets? 1
You can go to work
Take an umbrella

再次运行的输出:

Use boolean values(0 / 1) to answer the questions
Is it raining? 1
Do you have buses on the streets? 0
You cannot go to work

最后一次运行的输出:

Use boolean values(0 / 1) to answer the questions
Is it raining? 0
Do you have buses on the streets? 1
You can go to work
Enjoy the sun and have a nice day

第 15 行包含条件表达式 (isRaining && !busesPly),可将其读作 “下雨且没有公交车”。这个表达式使用了逻辑 AND 运算符将没有公交车(对有公交车执行逻辑 NOT 运算)和下雨关联起来。

以下程序演示了如何将逻辑运算符 NOT 和 OR( ! 和 ||)用于条件处理。

#include <iostream>
using namespace std;int main()
{cout << "Answer questions with 0 or 1" << endl;cout << "Is there a discount on your favorite car? ";bool onDiscount = false;cin >> onDiscount;cout << "Did you get a fantastic bonus? ";bool fantasticBonus = false;cin >> fantasticBonus;if (onDiscount || fantasticBonus)cout << "Congratulations, you can buy that car!" << endl;elsecout << "Sorry, waiting a while is a good idea" << endl;if (!onDiscount)cout << "Car not on discount" << endl;return 0;
}

输出:

Answer questions with 0 or 1
Is there a discount on your favorite car? 0
Did you get a fantastic bonus? 1
Congratulations, you can buy that car!
Car not on discount

再次运行的输出:

Answer questions with 0 or 1
Is there a discount on your favorite car? 0
Did you get a fantastic bonus? 0
Sorry, waiting a while is a good idea
Car not on discount

最后一次运行的输出:

Answer questions with 0 or 1
Is there a discount on your favorite car? 1
Did you get a fantastic bonus? 1
Congratulations, you can buy that car!  

分析:

这个程序建议您能够打折或得到很高的补贴时就把车买了,不然就再观望观望。它还在第 19 行使用了逻辑非运算来提醒您不打折。第 14 行使用了一条 if 语句,而第 16 行是与之配套的 else 语句。在条件 (onDiscount || fantasticBonus) 为 true 时,将执行第 15 行的语句。这个表达式包含逻辑运算符||,仅当您喜欢的汽车打折或能够获得很高的补贴时, 该表达式才为 true。当该表达式为 false 时, 将执行 else语句后面的语句(第 17 行)。

该文章会更新,欢迎大家批评指正。

推荐一个零声学院免费公开课程,个人觉得老师讲得不错,
分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,
fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,
TCP/IP,协程,DPDK等技术内容,点击立即学习:
服务器课程:C++服务器

学习链接

http://www.lryc.cn/news/132783.html

相关文章:

  • CloudQuery实战 | 谁说没有一款一体化数据库操作管控云平台了?
  • 如何编写一个通用的函数?
  • uni-app封装api请求
  • 深度学习从入门到实际项目资料汇总
  • 单元测试到底是什么?应该怎么做?
  • JavaWeb-Listener监听器
  • js数组常用的方法(总结)
  • Linux:shell脚本:基础使用(5)《正则表达式-sed工具》
  • 关于ubuntu下面安装cuda不对应版本的pyTorch
  • 【SA8295P 源码分析】26 - QNX Ethernet MAC 驱动 之 emac_rx_thread_handler 数据接收线程 源码分析
  • 70 # 协商缓存的配置:通过修改时间
  • 适合程序员的DB性能测试工具 JMeter
  • java实现人物关系抽取
  • Docker网络与资源控制
  • 图片怎么转换成pdf格式?可以试试这样转换
  • [国产MCU]-W801开发实例-GPIO输入与中断
  • Layui列表表头去掉复选框改为选择
  • Flutter实战·第二版-第三章 基础组件笔记
  • 一文彻底理解时间复杂度和空间复杂度(附实例)
  • Mysql的索引详解
  • .netcore windows app启动webserver
  • 泰迪大数据挖掘建模平台功能特色介绍
  • 【问题】java序列化,什么时候使用
  • 【最新可用】VMware中ubuntu与主机window之间使用共享文件夹传输大文件
  • A. Two Semiknights Meet
  • 〔011〕Stable Diffusion 之 解决绘制多人或面部很小的人物时面部崩坏问题 篇
  • 在ubuntu+cpolar+rabbitMQ环境下,实现mq服务端远程访问
  • Vue elementui 实现表格selection的默认勾选,翻页记录勾选状态
  • CloudCompare——统计滤波
  • nodejs+vue古诗词在线测试管理系统