# Control flow

Examples speak better than words:

  : foo if 42 else 54 then . ;

  0 foo --> prints 54
  1 foo --> prints 42

  : foo begin dup . 1- ?dup not until ;

  5 foo --> prints 54321

  : foo begin 'X' emit again ;

  foo --> prints X forever

  : foo begin ?dup while 1- dup . repeat ;

  5 foo --> prints 43210

A begin..repeat loop can have more than 1 while. In that case, things get weird
and it behaves like "if", that is that each while has its own "loop exit point".
Weird, but sometimes useful.

  : foo begin
    dup 10 <> while dup 15 <> while dup . 1+ repeat
    ( second while exit point ) .x1 else
    ( first while exit point ) . then ;

  5 foo --> prints 5678910
  11 foo --> prints 111213140f

The "case" structure allows us to "fan out" to many possible branches depending
on a single value. It's the functional equivalent of nested "if..then", but
more readable. Code like this:

  case
    1 = of ." foo!" endof
    2 > of ." bar!" endof
    drop ." baz!"
  endcase

is the exact equivalent of:

  r! 1 = if ." foo!" else
  r@ 2 > if ." bar!" else
  r@ drop ." baz!" then then

As you can see, the "base value" is automatically duplicated to PS before each
"of". The last part is the final "else", if no condition match. This branch also
has its base value duplicated, so even if you do nothing on the fallthrough
condition, you need to "drop" that value to keep PS balance.

During the execution of a "of" branch, the value in question is available
through "r@". This is an important gotcha when local variables [doc/usage/rs]
are used: case statements "use" a local variable slot.

None of these control structures above can be ran in interpret mode. They only
work when compiled. Sometimes, a little conditional in interpret mode can be
useful.

  42 [if] ." this will be executed" [then]
  0 [if] ." this won't" even if the code is nonsensical [then]

[if]..[then] work like comments and simply drop words until "[then]". There is
no "[else]".

On top of those words described above, Dusk also features a concept called
"iterators" which is awesome but needs a documentation page for itself.
See [doc/usage/iter].
