مشخصات مقاله
-
0.0
-
755
-
0
-
0
حلقه while loop در سالیدیتی
ابتدایی ترین حلقه در Solidity حلقه while است. هدف حلقه while اجرای مکرر یک دستور یا بلوک کد است تا زمانی که یک عبارت درست باشد. هنگامی که عبارت نادرست شد، حلقه خاتمه می یابد.
فلوچارت حلقه while
فلوچارت حلقه while به صورت زیر است:
سینتکس حلقه while در Solidity به صورت زیر است :
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);
}
}