File Metadata
- Created
- Wed, Jul 8, 10:18 PM
Control flow statements
These statements control execution flow of a program.
If-Else
The If-Else statement executes a main-expression branch if a specified condition is true.
If the condition is false, alternative-expression branch will be executed.
SYNTAX: if (condition):: type
{
[declaration. [ declaration.]... ]
main-expression
} else {
[declaration. [ declaration.]... ]
alternative-expression
}
condition
Bool type expression(see types) in order to decide which branch of IF-ELSE statement should be executed.
main expression
Executed if condition yelds true
alternative expression
Executed if condition yelds false
Example:
x = if (y<0) :: num
{-1} else {1}Switch
Executes one of the given branches depending on condition.
SYNTAX: switch (condition) :: type-of-switch
[case variant {
[declaration. [ declaration.]... ]
transformation-expression
}]...
condition
Expression to determing which case branch should be executed
type-of-switch
Type of switch result
variant
Expression to compare against condition.
If the condition evaluation result corresponds to the variant then a relevant branch is executed.
Special label default is allowed and designates default branch executed if no other branches are selected.
transformation-expression
Determines result of a switch statement if relevant branch is selected based on condition.
Example:
x = 2:: num.
y = switch (x) ::string
case 0 {"zero"}
case 1 {"one"}
case 2 {"two"}
case default {"unknown"}Map loop
Iterates over input collection and stores result in another collection.
Every input collection's value transformed using transformation-expression.
SYNTAX: loop map ( input-collection -> alias :: type-of-element) :: type-of-collection
{
[declaration. [ declaration.]... ]
transformation-expression
}
input-collection
Variable or expression to designate collection for transformation
alias
Variable name to designate current collection element in scope of inner code block
type-of-element
Type of collection's element
type-of-collection
Type of resulting transformed collection
transformation-expression
Transformation applied to an every collection element
Example:
input = [1..10].
twice = loop map (input -> x:: num):: [num]
{ 2 * x }Fold loop
Iterates over input collection in order to accumulate result by applying transformation-expression to each element and intermediate accumulator.
SYNTAX: loop fold (input-collection -> alias :: type-of-element, accumulator->accumulator-alias) :: type-of-accumulator
{
[declaration. [ declaration.]... ]
transformation-expression
}
input-collection
Variable or expression to designate collection
alias
Variable name to designate current collection element in scope of inner code block
type-of-element
Type of collection's element
accumulator
Expression to evaluate initial value of accumulator
accumulator-alias
Variable name to designate current value of accumulator in scope of inner code block
type-of-accumulator
Type of folding result
transformation-expression
Expression applied to an every collection's element and current state of accumulator in order to
calculate accumulator value for next step of iteration.
This is an example:
numbers = [1..10] :: [num].
sumOfNumbers = loop fold(numbers->x; 0->result)
{result + x}