Be lazy, Be crazy

If Else 본문

BlockChain/솔리디티

If Else

정신이 많이없는 개발자 2022. 3. 12. 18:18
728x90
반응형
솔리티디에서 ifElse와 삼항연산자를 사용하는 방법에 대한 코드를 작성
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;


contract IfElse {

      function example(uint _x) external pure returns (uint){
          
         if(_x < 10){
           return 1;
         }else if(_x <20){
             return 2;
         }
         return 3;
      }

      //삼항연산자(ternary operator)

      function ternary(uint _x) external pure returns (uint){

        //  if(_x <10){
        //      return 1;
        //  }
        //  return 2;

          return _x < 10 ? 1 : 2;
      }

}

 

주석친 부분과 삼항연산자로 작성된 부분은 동일한 로직이다.
반응형

'BlockChain > 솔리디티' 카테고리의 다른 글

Array  (0) 2022.03.15
FunctionOutputs  (0) 2022.03.14
FunctionModifier  (1) 2022.03.13
Error  (0) 2022.03.12
ForAndWhileLoops  (2) 2022.03.12
Comments