Source: https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/control-flow/ Documentation version: v5.3.2 # Control flow statements The control flow statements control the execution order of statements in a [procedure](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/procedures/create/). The following control flow statements are supported: - `WHILE`: executes a series of statements in a loop for as long as a specified condition is true. - `IF`: executes a series of statements based on a specified condition. - `BREAK`: exits a loop immediately. It is typically used within a WHILE statement or within an IF statement that is inside a loop. - `CONTINUE`: skips the remaining statements in the current iteration and proceeds to the next iteration. It is typically used within a WHILE statement or within an IF statement that is inside a loop. ## Syntax diagramsource while_statement if_statement break_statement continue_statement ```` while_statement ::= 'WHILE' value_expression 'THEN' '{' procedure_body '}' if_statement ::= 'IF' value_expression 'THEN' '{' procedure_body '}' ( 'ELSEIF' value_expression 'THEN' '{' procedure_body '}' )* ( 'ELSE' '{' procedure_body '}' )? break_statement ::= 'BREAK' continue_statement ::= 'CONTINUE' ```` ## Parameters ### `value_expression` A value expression that evaluates to a boolean value. For more information, see [Boolean type](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/data-types/boolean-type/) and [Expressions](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/fe/#value_expressions). ### `procedure_body` A set of statements to be executed. For more information, see [Procedure body](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/procedures/create/#procedure_body). ## Examples ### WHILE ```` ''' VALUE i INT = 0 WHILE i < 5 THEN { SET i = i + 1 } RETURN i ''' ```` Return: ```` +---+ | i | +---+ | 5 | +---+ ```` ### IF #### IF-ELSE ```` ''' VALUE i INT = 5 IF i < 5 THEN { SET i = i + 1 } ELSE { SET i = i - 1 } RETURN i ''' ```` Return: ```` +---+ | i | +---+ | 4 | +---+ ```` #### IF-ELSEIF-ELSE ```` ''' VALUE i INT = 4 IF i < 3 THEN { SET i = i + 1 } ELSEIF i < 5 THEN { SET i = i + 2 } ELSE { SET i = i - 1 } RETURN i ''' ```` Return: ```` +---+ | i | +---+ | 6 | +---+ ```` ### BREAK ```` ''' VALUE i INT = 0 WHILE i < 5 THEN { IF i = 3 THEN { BREAK } SET i = i + 1 } RETURN i ''' ```` Return: ```` +---+ | i | +---+ | 3 | +---+ ```` ### CONTINUE ```` ''' VALUE i INT = 0 WHILE i < 5 THEN { IF i < 3 THEN { SET i = i + 1 CONTINUE } ELSE { SET i = i + 1 } } RETURN i ''' ```` Return: ```` +---+ | i | +---+ | 5 | +---+ ````