Skip to main content

Statement Types

Leo supports several types of statements for controlling program flow and managing state.

Variable Declarations

Let Bindings

Declare immutable variables with let:
Type annotations are optional when the type can be inferred:

Constant Declarations

Declare compile-time constants with const:
Constants must be initialized with literal values or constant expressions. They cannot depend on runtime values.

Assignments

Simple Assignment

Reassign variables (for mutable contexts):

Destructuring Assignment

Unpack tuples and structs:

Conditional Statements

If Statements

Execute code based on conditions:

If-Else Statements

Provide alternative execution paths:

Nested Conditions

Combine multiple conditions:

Loops

For Loops

Iterate over a range of values:
Loop bounds must be known at compile time. Leo unrolls all loops during compilation.

Loop Examples

Assertions

Assert Statement

Verify conditions and fail if false:
Assertions in finalize functions:
If an assertion fails, the entire transaction is reverted. Use assertions for critical invariants and validation.

Return Statements

Simple Returns

Return a single value:

Multiple Returns

Return tuples for multiple values:

Early Returns

Return early from functions:

Block Statements

Scope Blocks

Create new scopes with curly braces:

Function Bodies

Function bodies are block statements:

Expression Statements

Expressions can be used as statements:

Statement Composition

Complex Control Flow

Combine statements for complex logic:

Best Practices

1. Initialize Variables

2. Use Descriptive Names

3. Limit Nesting Depth

4. Assert Preconditions Early

Common Patterns

Guard Clauses

State Machines

Accumulation

Next Steps

Functions

Build complete functions

Operators

Use operators in statements

Data Types

Work with different types

Finalize

On-chain state changes