Do I need to explicitly initialize the 'i' variable in a for loop? Since i is a uint and its default value is 0, why should I spend gas in a MSTORE operation to initialize the variable with 0? The same question applies to result.

function sum10() external pure returns (uint256) {
    uint256 result;
    for(uint256 i; i< 10; i++){
       result += i;
    return result;

This kind of things are simply easiest to try out in something like Remix.

I tried all three variants: as-is, explicitly setting i and explicitly setting result . The first two cost the same (25315), and the last one costs 25323.

Honestly, I'm really not sure why it's like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.