Understanding the if and if...else decision controls
Solidity provides conditional code execution with the help of the if...else
instructions. The general structure of if...else
is as follows:
if (this condition/expression is true) { Execute the instructions here } else if (this condition/expression is true) { Execute the instructions here } else { Execute the instructions here }
if
and if...else
are keywords in Solidity, and they inform the compiler that they contain a decision control condition – for example, if (a > 10)
. Here, if
contains a condition that can evaluate as either true
or false
. If a > 10
evaluates to true
, then the code instructions that follow in the pair of double-brackets, {
and }
, should be executed.
else
is also a keyword that provides an alternate path if none of the previous conditions are true
. It also contains a decision control instruction and executes the code instructions if...