정신이 많이없는 개발자 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;
      }

}

 

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