Solidity-do-while-loop

提供:Dev Guides
移動先:案内検索

Solidity-do …​ whileループ

*do ... while* ループは、ループの最後に条件チェックが発生することを除いて、 *while* ループに似ています。 つまり、条件が *false* であっても、ループは常に少なくとも1回は実行されます。

フローチャート

*do-while* ループのフローチャートは次のようになります-

Do Whileループ

構文

Solidityの do-while ループの構文は次のとおりです-

do {
   Statement(s) to be executed;
} while (expression);

- do …​ while ループの終わりで使用されるセミコロンをお見逃しなく。

Solidityで do-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;

      do {                  //do while loop
         bstr[k--] = byte(uint8(48 + _i % 10));
         _i/= 10;
      }
      while (_i != 0);
      return string(bstr);
   }
}

link:/solidity/solidity_first_application [Solidity First Application]の章に記載されている手順を使用して、上記のプログラムを実行します。

出力

0: string: 12