【For】

迴圈語法也和其他程式語言一模一樣。

pragma solidity ^0.8.11;

contract MyContract {
    uint[] numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

    function countEvenNumbers() public view returns (uint) {
        uint count = 0;
        
        for(uint i = 0; i < numbers.length; i++) {
            if(isEvenNumber(numbers[i])) {
                count ++;
            }
        }
        return count;
    }

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

Last updated