目录
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;
}



