# what is the different between this two? [closed]

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 {} 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.

• `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 '19 at 16:15
• 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 '19 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 '19 at 16:31
• 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 '19 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 '19 at 16:51

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.