语法
Solidity 中, do…while循环的语法如下:
do {
被执行语句(如果表示为真)
} while (表达式);
注意: 不要漏掉do后面的分号。
示例
pragma solidity ^0.5.0;
contract SolidityTest {
uint storedData;
constructor() public{
storedData = 10;
}
function getResult() public view returns(string memory){
uint a = 10;
uint b = 2;
uint result = a + b;
return integerToString(result);
}
function integerToString(uint _i) internal pure
returns (string memory) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len - 1;
do { // do while 循环
bstr[k--] = byte(uint8(48 + _i % 10));
_i /= 10;
}
while (_i != 0);
return string(bstr);
}
}
可以参考Solidity – 第一个程序中的步骤,运行上述程序。
输出
0: string: 12