栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【一起学C++】条件语句、逻辑运算符

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【一起学C++】条件语句、逻辑运算符

目录

if语句

switch语句

逻辑运算符

三向比较运算符


if语句

条件是指满足指定条件时,执行响应的逻辑。

if (i > 4) {
    // Do something.
} else if (i > 2) {
    // Do something else.
} else {
    // Do something else.
}

值得注意的是,if,else if, else三个代码直接是互斥的,也就是说,有且仅有一个会执行。

同样的,在c++中,条件判断需要是逻辑值,但可以使用0表示false,非0表示true,即下面是等价的:

if (true)
{
    // do something
}

if (1)
{
    // do something
}

if (-5)
{
    // do something
}
if (false)
{
    // do something
}

if (0)
{
    // do something
}

一种不太常规的使用方式,遇到能够理解就可以,不是最佳实践:

if (; ) {
    
} else if () {
    
} else {
    
}

示例:

if (Employee employee { getEmployee() }; employee.salary > 1000) 
{ 
    ... 
}

这里定义的变量,在if内部均可使用(, , , , .),在外部是获取不到的。

switch语句

switch适用于整型,枚举。遇到break会停止,否则继续执行。default是其他条件都无法满足时的替代方案。

switch (menuItem) {
    case OpenMenuItem:
        // Code to open a file
        break;
    case SaveMenuItem:
        // Code to save a file
        break;
    default:
        // Code to give an error message
        break;
}

一般来说,Switch语句是可以替换成对应的if语句的,例如上述的switch语句是可以改写的:

if (menuItem == OpenMenuItem) {
    // Code to open a file
} else if (menuItem == SaveMenuItem) {
    // Code to save a file
} else {
    // Code to give an error message
}

不使用break时,会发生fallthrough:

enum class Mode { Default, Custom, Standard };

int value { 42 };
Mode mode {  };

switch (mode) {
    using enum Mode;

    case Custom:
        value = 84;
    case Standard:
    case Default:
        // Do something with value ...
        break;
}

当Custom满足时,Custom,Standard,Default逻辑都会执行,这种很形象的称为fallthrough(贯穿)

一般编译器会给出warning,可以使用[[fallthrough]]进行压制:

switch (mode) {
    using enum Mode;

    case Custom:
        value = 84;
        [[fallthrough]];
    case Standard:
    case Default:
        // Do something with value ...
        break;
}

当然,switch也有初始化的代码,但也同样不是最佳实践:

switch (; ) 
{ 
     
}

逻辑运算符
操作符含义示例
<
<=
>
>=

小于

小于等于

大于

大于等于

if (i < 0) {
        cout << "i is negative";
}
==相等判断if (i == 3) {
        cout << "i is 3";
}
!=不相等判断if (i != 3) {
        cout << "i is not 3";
}
<=>三向比较运算符result = i <=> 0;
逻辑非if (!someBoolean) {
        cout << "someBoolean is false";
}
&&

逻辑且,

同时满足才满足

if (someBoolean && someOtherBoolean) {
        cout << "both are true";
}
||

逻辑或,

一个满足即满足

if (someBoolean || someOtherBoolean) {
        cout << "at least one is true";
}

需要注意的是,c++的逻辑且,逻辑或均是短路的,意思是:

1. 如果一个连续的逻辑或在靠左侧的位置能够确定true,则后续判断不再执行。

2. 如果一个连续的逻辑且在靠左的位置能够确定false,则后续的判断也不再执行。

三向比较运算符

对于整型的比较:

A <=> B 

strong_ordering::less 表示A小于B
strong_ordering::greater 表示A大于B
strong_ordering::equal 表示A等于B

对于浮点型的比较:

A <=> B

partial_ordering::less 表示A小于B
partial_ordering::greater 表示A大于B
partial_ordering::equivalent 表示A等于B
partial_ordering::unordered 表示A或者B不是数字

自定义类型的比较:

A <=> B

weak_ordering::less 表示A小于B
weak_ordering::greater 表示A大于B
weak_ordering::equivalent 表示A等于B

判断结果的示例:

int i { 11 };
strong_ordering result { i <=> 0 };

if (result == strong_ordering::less) 
{ 
    cout << "less" << endl; 
}

if (result == strong_ordering::greater) 
{ 
    cout << "greater" << endl; 
}

if (result == strong_ordering::equal) 
{ 
    cout << "equal" << endl; 
}

也可以使用下面的方法判断结果:

std::is_eq()等于
std::is_neq()不等于
std::is_lt()小于
std::is_lteq()小于等于
std::is_gt()大于
std::is_gteq()大于等于

示例:

strong_ordering result { i <=> 0 };

if (is_lt(result)) 
{ 
    cout << "less" << endl; 
}

if (is_gt(result)) 
{ 
    cout << "greater" << endl; 
}

if (is_eq(result)) 
{ 
    cout << "equal" << endl; 
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/886814.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号