Programming Language/Solidity
Solidity - If / Else
Yongari
2022. 12. 23. 19:07
Solidity supports conditional statements if, else if and 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;
// shorthand way to write if / else statement
// the "?" operator is called the ternary operator
// 솔리디티는 삼항연산자를 지원합니다.
return _x < 10 ? 1 : 2;
}
}