statements.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. Statements
  2. ==========
  3. In functions, you can declare variables in code blocks. If the name is the same as
  4. an existing function, enum type, or another variable, then the compiler will shadow
  5. the original item and generate a warning as it is no longer accessible.
  6. .. include:: ../examples/shadowing.sol
  7. :code: solidity
  8. Scoping rules apply as you would expect, so if you declare a variable in a block, then it is not
  9. accessible outside that block. For example:
  10. .. include:: ../examples/scoping.sol
  11. :code: solidity
  12. If statement
  13. ____________
  14. Conditional execution of a block can be achieved using an ``if (condition) { }`` statement. The
  15. condition must evaluate to a ``bool`` value.
  16. .. include:: ../examples/statement_if.sol
  17. :code: solidity
  18. The statements enclosed by ``{`` and ``}`` (commonly known as a *block*) are executed only if
  19. the condition evaluates to true.
  20. You can optionally add an ``else`` block which is executed only if the condition evaluates to false.
  21. .. include:: ../examples/statement_if_else.sol
  22. :code: solidity
  23. While statement
  24. _______________
  25. Repeated execution of a block can be achieved using ``while``. It syntax is similar to ``if``,
  26. however the block is repeatedly executed until the condition evaluates to false.
  27. If the condition is not true on first execution, then the loop body is never executed:
  28. .. include:: ../examples/statement_while.sol
  29. :code: solidity
  30. It is possible to terminate execution of the while statement by using the ``break`` statement.
  31. Execution will continue to next statement in the function. Alternatively, ``continue`` will
  32. cease execution of the block, but repeat the loop if the condition still holds:
  33. .. include:: ../examples/statement_while_break.sol
  34. :code: solidity
  35. Do While statement
  36. __________________
  37. A ``do { ... } while (condition);`` statement is much like the ``while (condition) { ... }`` except
  38. that the condition is evaluated after executing the block. This means that the block is always executed
  39. at least once, which is not true for ``while`` statements:
  40. .. include:: ../examples/statement_do_while.sol
  41. :code: solidity
  42. For statements
  43. ______________
  44. For loops are like ``while`` loops with added syntaxic sugar. To execute a loop, we often
  45. need to declare a loop variable, set its initial variable, have a loop condition, and then
  46. adjust the loop variable for the next loop iteration.
  47. For example, to loop from 0 to 1000 by steps of 100:
  48. .. include:: ../examples/statement_for.sol
  49. :code: solidity
  50. The declaration ``uint i = 0`` can be omitted if no new variable needs to be declared, and
  51. similarly the post increment ``i += 100`` can be omitted if not necessary. The loop condition
  52. must evaluate to a boolean, or it can be omitted completely. If it is ommited the block must
  53. contain a ``break`` or ``return`` statement, else execution will
  54. repeat infinitely (or until all gas is spent):
  55. .. include:: ../examples/statement_for_abort.sol
  56. :code: solidity
  57. .. _destructuring:
  58. Destructuring Statement
  59. _______________________
  60. The destructuring statement can be used for making function calls to functions that have
  61. multiple return values. The list can contain either:
  62. 1. The name of an existing variable. The type must match the type of the return value.
  63. 2. A new variable declaration with a type. Again, the type must match the type of the return value.
  64. 3. Empty; this return value is ignored and not accessible.
  65. .. include:: ../examples/statement_destructing.sol
  66. :code: solidity
  67. The right hand side may also be a list of expressions. This type can be useful for swapping
  68. values, for example.
  69. .. include:: ../examples/statement_destructing_swapping.sol
  70. :code: solidity
  71. The right hand side of an destructure may contain the ternary conditional operator. The number
  72. of elements in both sides of the conditional must match the left hand side of the destructure statement.
  73. .. include:: ../examples/statement_destructing_conditional.sol
  74. :code: solidity
  75. .. _try-catch:
  76. Try Catch Statement
  77. ___________________
  78. Solidity's try-catch statement can only be used with external calls or constructor calls using ``new``. The
  79. compiler will refuse to compile any other expression.
  80. Sometimes execution gets reverted due to a ``revert()`` or ``require()``. These types of problems
  81. usually cause the entire transaction to be aborted. However, it is possible to catch
  82. some of these problems in the caller and continue execution.
  83. This is only possible for contract instantiation through new, and external function calls.
  84. An internal function cannot be called from a try catch statement. Not all problems can be handled,
  85. for example, out of gas cannot be caught. The ``revert()`` and ``require()`` builtins may
  86. be passed a reason code, which can be inspected using the ``catch Error(string)`` syntax.
  87. .. warning::
  88. On Solana, any transaction that fails halts the execution of a contract. The try-catch statement, thus,
  89. is not supported for Solana contracts and the compiler will raise an error if it detects its usage.
  90. .. include:: ../examples/substrate/statement_try_catch_constructor.sol
  91. :code: solidity
  92. The same statement can be used for calling external functions. The ``returns (...)``
  93. part must match the return types for the function. If no name is provided, that
  94. return value is not accessible.
  95. .. include:: ../examples/substrate/statement_try_catch_call.sol
  96. :code: solidity
  97. There is an alternate syntax which avoids the abi decoding by leaving the `catch Error(…)` out.
  98. This might be useful when no error string is expected, and will generate shorter code.
  99. .. include:: ../examples/substrate/statement_try_catch_no_error_handling.sol
  100. :code: solidity