【While】

迴圈語法也和其他程式語言一模一樣,以下這個例子跟上面 for 迴圈的例子一模一樣。

pragma solidity ^0.8.11;

contract MyContract {
    uint[] public numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    function countEvenNumbers() public view returns (uint) {
        uint count = 0;
        uint i = 0;
        while(i < numbers.length){
            if(isEvenNumber(numbers[i])) {
                count ++;
            }
            i ++;
        }
        return count;
    }

    function isEvenNumber(uint _number) public view returns(bool) {
        return (_number % 2 == 0 ? true : false);
    }
}

Last updated