Skip to content

Latest commit

 

History

History
55 lines (54 loc) · 1.09 KB

File metadata and controls

55 lines (54 loc) · 1.09 KB

控制流,用Solidity实现插入排序

控制流

solidity的控制流和其他语言类似,主要包含以下几种

  1. if-else
function ifElseTest(uint256 _number) public pure returns(bool){
    if(_number == 0){
        return(true);
    }else{
        return(false);
    }
}
  1. for循环
function forLoopTest() public pure returns(uint256){
    uint sum = 0;
    for(uint i = 0; i < 10; i++){
        sum += i;
    }
    return(sum);
}
  1. while循环
function whileTest() public pure returns(uint256){
    uint sum = 0;
    uint i = 0;
    while(i < 10){
        sum += i;
        i++;
    }
    return(sum);
}
  1. do-while循环
function doWhileTest() public pure returns(uint256){
    uint sum = 0;
    uint i = 0;
    do{
        sum += i;
        i++;
    }while(i < 10);
    return(sum);
}
  1. 三元运算符:三元运算符是Solidity中唯一一个接受三个操作数的运算符
function ternaryTest(uint256 x, uint256 y) public pure returns(uint256){
    // return the max of x and y
    return x >= y ? x: y; 
}