笔记27:C++中是否有判断的链式法则

一.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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值