query_script_loop_while

QueryScript Flow Control: loop-while statement

SYNOPSIS

loop
  statement;
while (expression);

DESCRIPTION

loop-while is a flow control looping structure. It makes for a condition test based on a given expression.

As opposed to while, loop-while tests the expression after each statement execution. As result, the loop iterates at least once.

The loop-while loop terminates in the following cases:

  • The expression does not hold true
  • A break statement is executed inside the loop (but not inside a nested loop)
  • A return statement is executed inside the loop
  • An uncaught error is raised (at the moment all errors are by default uncaught unless handled in user code)
Of course, any error terminates the entire script execution, including any loops.

Empty statements are not allowed in QueryScript. However, empty blocks are, and the loop-while clause may be followed by an empty block, or by the do-nothing pass statement.

EXAMPLES

Generate Fibonacci sequence:

var $n1, $n2, $n3, $seq;
set $n1 := 1, $n2 := 0, $n3 := NULL;
set $seq := '';

loop
{
  set $n3 := $n1 + $n2;
  set $n1 := $n2;
  set $n2 := $n3;
  set $seq := CONCAT($seq, $n3, ', ');
}
while ($n3 < 100);

SELECT $seq AS fibonacci_numbers;

+---------------------------------------------+
| fibonacci_numbers                           |
+---------------------------------------------+
| 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,  |
+---------------------------------------------+

SEE ALSO

if-else, while, foreach, expressions, break

AUTHOR

Shlomi Noach
 
common_schema documentation