Imperative Blocks

A block of code enclosed into braces is called imperative:

'{' ... '}'

An imperative block may appear as a statement, or as part of phrase.

The imperative code consists of sentences, just like a declarative one.

Any nested block inside of imperative one, either enclosed into parentheses or braces, is imperative too.

An imperative code, in contrast to declarative one, can not declare the enclosing object’s members. But it can contain an advanced execution logic, such as loops.

A declarative within imperative code is called imperative sentence and is treated the same way.

Named Blocks

A name can be assigned to imperative block when it appears as a statement. The syntax is:

<name> ':' '{' ... '}'

The block name should be unique within an object definition or within a phrase part.

Loops

Every imperative block is inherently a loop, because every block execution can be repeated or aborted.

An imperative block is repeated after successful execution of continuation sentence terminated with ellipsis (...):

$I = ``0
{ ~~ Loop.
  Print [i] nl ~~ Prints numbers from `0` to `9`.
  I +<< 1
  I < 10? ...  ~~ Repeat the loop if `i < 10`.
}

Continuations ignore parentheses. So, the above can be written as following:

$I = ``0
{ ~~ Loop.
  Print [i] nl  ~~ Prints numbers from `0` to `9`.
  I +<< 1
  I < 10? (...) ~~ Repeat the loop if `i < 10`.
                ~~ Note that execution control escapes parentheses
                ~~ and continues execution from the opening brace.
}

If empty continuation sentence immediately follows an interrogative one, a short continued interrogation (?..) syntax can be used:

$I = ``0
{ ~~ Loop.
  Print [i] nl ~~ Prints numbers from `0` to `9`.
  I +<< 1
  I < 10?..    ~~ Repeat the loop if `i < 10`.
}

It is possible to repeat the execution of any enclosing block, not only the immediately enclosing one. For that the target block should have a name, and a special ellipsis syntax should be used:

$I = ``0
Outer loop: {
  $J = ``i
  I < 10 ? { ~~ Inner loop.
    J > 0? Print " "
    Print [i]
    J -<< 1
    J < 0? I +<< 1, print _nl... outer loop ~~ Repeat the `outer loop`.
    ... ~~ Repeat the inner loop.
  }
}

An imperative block is exited after successful execution of exclamation sentence terminated with exclamation mark (!).

$I = ``0
{ ~~ Loop.
  Print [i] nl ~~ Prints numbers from `0` to `9`.
  I +<< 1
  I >= 10?!    ~~ Exit when `i >= 10`.
  ...          ~~ Repeat the loop otherwise.
}

To exit a named block a special continued exclamation (!..) syntax should be used:

$I = ``0
Outer loop: {
  $J = ``i
  { ~~ Inner loop.
    J > 0? Print " "
    Print [i]
    J -<< 1
    J > 0?..                 ~~ Repeat the inner loop.
    I +<< 1
    Print _nl
    I >= 10?!.. outer loop   ~~ Exit the `outer loop`.
    ... outer loop           ~~ Repeat the `outer loop`.
  }
}