Skip to content

4.1. Blocks#

Syntax#


Block = (* open_brace: *) OPEN_BRACE
(* statements: *) Statements
(* close_brace: *) CLOSE_BRACE;

Statements = (* item: *) Statement*;

Statement = (* variant: *) IfStatement
| (* variant: *) ForStatement
| (* variant: *) WhileStatement
| (* variant: *) DoWhileStatement
| (* variant: *) ContinueStatement
| (* variant: *) BreakStatement
| (* variant: *) ReturnStatement
| (* variant: *) ThrowStatement (* Deprecated in 0.5.0 *)
| (* variant: *) EmitStatement (* Introduced in 0.4.21 *)
| (* variant: *) TryStatement (* Introduced in 0.6.0 *)
| (* variant: *) RevertStatement (* Introduced in 0.8.4 *)
| (* variant: *) AssemblyStatement
| (* variant: *) Block
| (* variant: *) UncheckedBlock (* Introduced in 0.8.0 *)
| (* variant: *) TupleDeconstructionStatement
| (* variant: *) VariableDeclarationStatement
| (* variant: *) ExpressionStatement;

(* Introduced in 0.8.0 *)
UncheckedBlock = (* unchecked_keyword: *) UNCHECKED_KEYWORD
(* block: *) Block;

ExpressionStatement = (* expression: *) Expression
(* semicolon: *) SEMICOLON;

AssemblyStatement = (* assembly_keyword: *) ASSEMBLY_KEYWORD
(* label: *) StringLiteral?
(* flags: *) AssemblyFlagsDeclaration?
(* body: *) YulBlock;

AssemblyFlagsDeclaration = (* open_paren: *) OPEN_PAREN
(* flags: *) AssemblyFlags
(* close_paren: *) CLOSE_PAREN;

AssemblyFlags = (* item: *) StringLiteral ((* separator: *) COMMA (* item: *) StringLiteral)*;

Unchecked Blocks#

Starting with v0.8.0, by default, all arithmetic operations are checked for underflow or overflow, which means that if the result of an operation falls outside the value range of the type, the call is reverted through a failing assertion. This can be disabled using the unchecked block, resulting in wrapping arithmetic:

unchecked {
  i++;
}

Note

This section is under construction. You are more than welcome to contribute suggestions to our GitHub repository.