-1

today I have two type

One is like this

pragma solidity ^0.5.0;

contract Test{

  function TestVar() view public returns(uint8){
    uint8 a;
    for(uint8 b=0;b<20; b++){
      a=b;
    }
    return a;
  }    
}

When I test this function,it would return 19. This is very easy to understand.

pragma solidity ^0.5.0;

contract Test{

  function TestVar() view public returns(uint8){
    uint8 a;
    for(uint8 b=0;b<20; b++){
      a=b;
      return a;
    }
  }   
}

But if I put this return a; in this place,it would return 0. Why? can someone tell me the logic?

thanks!!

Edit after @Jaime & @Richard Horrocks answering,thanks for these kindly guy!

So you mean that if I put a {} behindfor(uint8 b=0;b<20; b++) it would run before the for roop?

pragma solidity ^0.5.0;

contract Test{

    function TestVar() view public returns(uint8){
      uint8 a;
      for(uint8 b=0;b<20; b++)
        a=b;
      return a;
    }

}

Like this ,I don't use {},and we can find the return a is 19.

closed as off-topic by Ismael, shane, Nicolas Massart, Rosco Kalis, Rob Hitchens - B9lab Feb 26 at 15:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Ethereum, the decentralized application platform and smart contract enabled blockchain, within the scope defined in the help center." – Ismael, shane, Nicolas Massart, Rosco Kalis, Rob Hitchens - B9lab
If this question can be reworded to fit the rules in the help center, please edit the question.

  • return a will return the value of a and exit, therefore in the for loop the first value for b is zero, a = b makes a =0 and then the code exits returning the current value in a. – Jaime Feb 25 at 16:15
  • 1
    Because you're returning from the function inside the first iteration of the loop. The loop is therefore only run once, with b (and then a) set to 0 (its starting value). – Richard Horrocks Feb 25 at 16:15
  • Thanks for @Jaime & @Richard Horrocks So you mean that if I put a {} behind` for(uint8 b=0;b<20; b++)` it would run before the for roop? pragma solidity ^0.5.0; contract Test{ function TestVar() view public returns(uint8){ uint8 a; for(uint8 b=0;b<20; b++) a=b; return a; } } Like this ,I don't use {},and we can find the return a is 19 – user50988 Feb 25 at 16:31
  • 3
    The question implies that you have never used any other programming language whatsoever. While this is feasible, I believe that it is not very common (or "natural") to take on blockchain-programming before you've acquainted yourself with languages such as Python, Java, Javascript, or even C, with which you can gather some experience on a more simple environment to begin with. – goodvibration Feb 25 at 16:43
  • Acually,I am a programming noob haha! So you mean that I can learn other common language before learning solidity? Or It is still possible that I can learn solidity directly and then learn the other? – user50988 Feb 25 at 16:51
1

If you remove the {} then the for loop will only execute the next statement. i.e a=b. Then when it completes, the return statement is executed hence you have value of 19.

  • thanks for @user3316669 I check that one more time So when I remove{} ,it would execute the for loop, and then finds that a=b,and finally return a ,right? And when I add {} ,it would execute the code in the {},then execute the for loop,right? – user50988 Feb 25 at 16:43
  • According to your code. You’ve removed the block, {}. So it will execute the next statement which is assigning a the value of b for each iteration of the loop. Hence the return value of a will be the last value assigned by b. – user3316669 Feb 25 at 18:16

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