打开智能合约在线编辑器,创建编译部署执行下面合约代码即可演练Solidity中布尔数据类型与逻辑运算
Boolean.sol
pragma solidity ^0.4.0;
contract BooleanTest {
bool _a; // 1. bool数据类型未初始化时默认返回false
int num1 = 100;
int num2 = 200;
function getBool() returns(bool) {
return _a;
}
function getBool2() returns(bool) {
return !_a; // 2. 前面加!布尔值取反
}
function judge() returns(bool) {
return num1 == num2; // 3. 用于判断
}
function judge2() returns(bool) {
return num1 != num2;
}
// 4. 与或非,&& || !
function and() returns(bool) {
return (num1 == num2) && true; // false
}
// ||
function or() returns(bool) {
return (num1 == num2) || true; // true
}
}