作者:黎躍春,區塊鏈、高可用架構工程師
微信:liyc1215 QQ羣:348924182 博客:http://liyuechun.org
布爾(Booleans)
bool:
可能的取值爲常量值true
和false
。
支持的運算符:
!
邏輯非&&
邏輯與||
邏輯或==
等於!=
不等於
備註:運算符&&
和||
是短路運算符,如f(x)||g(y)
,當f(x)
爲真時,則不會繼續執行g(y)
在f(x)&&g(y)
表達式中,當f(x)
爲false
時,則不會執行g(y)
。
bool a = true;
bool b = !a;
// a == b -> false
// a != b -> true
// a || b -> true
// a && b -> false
案例
pragma solidity ^0.4.4;
contract Test {
uint _a;
uint _b;
bool _c;
function Test() {
_a = 100;
_b = 200;
_c = true;
}
// ! 邏輯非
function fei() constant returns (bool) {
return (!_c);
}
// && 邏輯與
// == 等於
function luojiyu() constant returns (bool) {
return _a == _b && _c;
}
// || 邏輯或
// != 不等於
function luojihuo() constant returns (bool) {
return _a != _b || _c;
}
}