If / Else
Solidity中支持条件语句if, else if和else.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract IfElse {
function foo(uint x) public pure returns (uint) {
if (x < 10) {
return 0;
} else if (x < 20) {
return 1;
} else {
return 2;
}
}
function ternary(uint _x) public pure returns (uint) {
// if (_x < 10) {
// return 1;
// }
// return 2;
// if / else 语句的一种简写方式,有时候也称之为问号表达式
// 其中"?"操作符为三元操作符
return _x < 10 ? 1 : 2;
}
}