Control structures in Solidity
Most of the control structures from other languages are also supported in solidity. In this recipe, you will learn about supported control structures in solidity, along with examples. The semantics are very similar to C or JavaScript.
How to do it...
- If-else condition statements are used to perform different actions based on different conditions. Create a function,
isValid
, which returnstrue
for input values greater than 10 and returnsfalse
otherwise:
pragma solidity ^0.4.23; contract test { function isValid(uint input) public pure returns (bool) { if (input > 10) { return true; } else { return false; } } }
Solidity doesn't support type conversion from Boolean to non-Boolean types as in other languages like JavaScript. So, if(1) { } is not a valid condition in solidity.
- A
while loop
allows code to be executed repeatedly based on a Boolean condition. Create a while loop to send Ether to all addresses...