Solidity-while-loops
提供:Dev Guides
堅牢性-Whileループ
Solidityの最も基本的なループは、この章で説明する while ループです。 while ループの目的は、 expression が真である限り、ステートメントまたはコードブロックを繰り返し実行することです。 式が false になると、ループは終了します。
フローチャート
- whileループ*のフローチャートは次のようになります-
構文
Solidityの* whileループ*の構文は次のとおりです-
while (expression) {
Statement(s) to be executed if expression is true
}
例
whileループを実装するには、次の例を試してください。
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;
while (_i != 0) {//while loop
bstr[k--] = byte(uint8(48 + _i % 10));
_i/= 10;
}
return string(bstr);
}
}
link:/solidity/solidity_first_application [Solidity First Application]の章に記載されている手順を使用して、上記のプログラムを実行します。
出力
0: string: 12