Skip to main content

Loops and iterations

A smart contract can contain loops, which take two general forms:

  • Conditional loops, which keep iterating as long as a given condition is true, such as while loops
  • Loops that iterate through every element of a data structure such as a list, map, or set
warning

When using loops, be careful of attacks that could increase the number of iterations in the loop.

In many cases, it is possible to avoid performing loops in the contract itself by doing most of the computations off-chain.

Implementation details