一.python中的链式法则
eg1:正常输出aaaa
x = 2
if 1 < x < 3:
print('aaaa')
eg2:不输出任何值
x = 2
if 1 < x < 2:
print('aaaa')
解释:因为python对于同一优先级的判断符号【>,<,>=,<=,==】,具有链式法则。程序中的【1 < x < 2】其实就是【(1 < x) && (x < 2)】,python自动将其拆分为了两个子条件
a
a
a
a
二.C++中的链式法则
注意:C++并不存在判断的链式法则
eg1:输出aaaa
#include <iostream>
using namespace std;
int main()
{
int x = 2;
if (1 < x < 3)
{
cout << "aaaa";
}
system("pause");
return 0;
}
解释:因为C++不存在链式法则,所以程序【1 < x < 3】其实就是【(1 < x) < 3】,所以(1 < x)被判别为True,而布尔类型的值True就是值1,然后再判断(1 < 3),是True则输出aaaa
eg2:输出aaaa
#include <iostream>
using namespace std;
int main()
{
int x = 2;
if (1 < x < 2)
{
cout << "aaaaaa";
}
system("pause");
return 0;
}
eg3:不输出任何结果
#include <iostream>
using namespace std;
int main()
{
int x = 2;
if (1 < x > 2)
{
cout << "aaaaaa";
}
system("pause");
return 0;
}