Flow Control
In Dewy, the two main methods of conditionally executing code are if
and loop
expressions.
If Expressions
If expressions allow you to conditionally evaluate code based on whether or not some condition is met.
The syntax for an if
expression is:
where <condition>
must result in a boolean value, and <expression>
can be anything. Commonly, <expression>
will be a block containing multiple expressions.
Loop Expressions
Loop expressions allow you to repeat the execution of some code while some condition is met.
The syntax for a loop
expression is:
where <condition>
must be an expression that evaluates to a boolean value, and <expression>
can be anything.
Loops will be explored in more detail in One Loop To Rule Them All.
Flow Chains
Multiple flow expressions can be chained together via the else
operator, along with an optional final default case that need not be a flow expression. In normal languages, this would be if-else-if
kinds of sequences, which are certainly possible in Dewy:
or even
But Dewy also allows loop
expressions to be combined in this way as well:
In the above example, if a
is greater than b
, the first block would be executed, and the rest of the blocks are skipped. If a
is less than b
, the loop in the second block executes, incrementing a
until it is equal to b
, at which point the rest of the chain is skipped. Only if a
is neither greater than nor less than b
(i.e. a
equals b
) will the final block be executed exclusively.
(TODO: add an example for how all conditions share the same scope, so variables defined in one condition will be available in later bodies if they execute)
(TODO: probably add a finally
operator which can be used to always execute code at the end)
Capturing Values
Unlike if statements from other languages, if
s and loop
s in Dewy are themselves expressions, allowing any expressed values to be captured. if
expressions basically act like Dewy's version of the ternary operator
my_var
would have a value of 'a tropical fruit'
at the end of the above example.
Values from loops can be captured to construct sequences, which is explored more in One Loop To Rule Them All.
Match Expressions
(TODO) switch statement equivalent
Break, Continue, Return,
(TODO) branch inside body of conditional
Advanced Flow Control
(TODO) combining conditionals (TODO) list and dictionary generators