Control Flow
LichenScript provides a variety of control flow statements.
These include while
loops to perform a task multiple times;
if
and match
expression to execute different branches of code based on certain conditions; and statements such as break and continue to transfer the flow of execution to another point in your code.
If
In its simplest form, the if
expression has a single if
condition.
It executes a set of statements only if that condition is true
.
if num % i == 0 { counter += 1;}
Unlike most common C-family languages,
if
is an expression in LichenScript.
You can use it in another expression.
const max = if a > b { a } else { b };print(max);
While
A while
loop starts by evaluating a single condition.
If the condition is true
,
a set of statements is repeated until the condition becomes false
.
while nextTerm < 100 { t1 = t2; t2 = nextTerm; nextTerm = t1 + t2;}
For
A for
statement is used to iterate a iterable object such as array and map.
const a = [1,2,3,4,5,6,7,8];for item in a { print("item:", item);}