Solidity Control Structures

In this article, we’ll take a closer look at control structures in Solidity. It’s part of our long-standing tradition to make this (and other) articles a faithful companion, or a supplement to the official Solidity documentation.

You can watch our explainer video here:

So far, we’ve been observing various areas of Solidity, with units and globally available variables being the most recent ones. As we’re gaining more familiarity with Solidity, we continue diving even deeper, this time into its moving parts.

The first on our list are control structures, which we may consider as elements that enable Solidity to behave in a non-linear way.

Control Structures

Speaking from a syntax perspective, Solidity inherits its control structures from its influences, the so-called “curly braces” programming languages.

These are the languages with programming constructs whose body is delineated with curly braces, { and }.

Here’s an example in Solidity:

<a_control_structure_keyword> (a conditional expression)
{
   <a_control_structure_body_line_1>
   ...
   <a_control_structure_body_line_N>
}

Control Structure Keywords

These programming languages, such as the (seemingly immortal) C, Java and C++, JavaScript, use familiar programming constructs based on keywords, such as

  • if,
  • else,
  • while,
  • do,
  • for,
  • break,
  • continue, and
  • return.

Control Structure Exception Handling

Besides these keywords, Solidity also supports somewhat more modern, but by no means new construct try/catch keywords for exception handling.

πŸ’‘ Exception handling in this manner is reserved only for external function calls and contract creation calls. In this context, we can use the revert() statement to throw errors when required.

Conditionals

On a syntactical note, when working with conditional expressions, or in short just conditionals, we cannot omit the parentheses. In contrast, if the body of a control structure contains only one statement, curly braces can be left out.

Here’s an example:

<a_control_structure_keyword> (a conditional expression)
   <a_control_structure_body_single_line>

Or, more specifically:

if (a == 1)
   b = 2;

πŸ—’οΈ Note: In some programming languages, such as the ones mentioned above, we can stumble upon code snippets similar to if (1) {...}. In those languages, such expression is interpreted as if (true) {...}, because they support conversion from non-boolean to boolean type. However, Solidity does not support type conversion from non-boolean to boolean type, and such code snippets in Solidity are marked invalid.

Conclusion

In this article, we made ourselves familiar with control structures.

We learned about what control structures are.

Also, we discovered what keywords are available to form a control structure in Solidity, and in what situations we can omit the curly braces and still have a working, valid control structure.

What’s Next?

This tutorial is part of our extended Solidity documentation with videos and more accessible examples and explanations. You can navigate the series here (all links open in a new tab):