Control Flow

Control Flow. #

Cyber provides the common constructs to branch and enter loops.

Branching. #

Use if and else statements to branch the execution of your code depending on conditions. The else clause can contain a condition which is only evaluated if the previous if or conditional else clause was false.

var a = 10
if a == 10:
    print 'a is 10'
else a == 20:
    print 'a is 20'
    print 'neither 10 nor 20'

An if expression also needs the then keyword. Conditional else clauses are not allowed in an if expression.:

var a = 10
var str = if a == 10 then 'red' else 'blue'

Use and and or logical operators to combine conditions:

var a = 10
if a > 5 and a < 15:
    print 'a is between 5 and 15'
if a == 20 or a == 10: 
    print 'a is 10 or 20'

Iterations. #

Infinite and conditional loops start with the while keyword. An infinite loop continues to run the code in the block until a break or return is reached. When the while clause contains a condition, the loop continues to run until the condition is evaluated to false.

-- Infinite loop.

var running = true
while running:
    -- Keep looping until `running` is false.

You can use the optional while loop to continue the loop until the expression evaluates to the none value. The unwrapped optional value is copied to the variable declared after some.

var iter = dir.walk()
while some entry:

for loops can iterate over a range that starts at a int (inclusive) to a target int (exclusive). The range can be given a custom step.

for 0..100 each i:
    print i    -- 0, 1, 2, ... , 99

for 0..100, 10 each i:
    print i    -- 0, 10, 20, ... , 90

for 100..0, 1 each i:
    print i    -- 100, 99, 98, ... , 1

When the range operator .. is replaced with ..=, the target int is inclusive.

Planned Feature

for 100..=0, 1 each i:
    print i    -- 100, 99, 98, ... , 0

The for clause can iterate over an Iterable object. An Iterable type contains an iterator() method that returns an Iterator object. An Iterator type contains a next() method that returns the next value or none when finished. You can iterate lists since they are Iterable.

var list = [1, 2, 3, 4, 5]

-- Iterate on values.
for list each n:
    print n

When the as clause contains a sequence destructuring expression, the for loop will iterate a SequenceIterator created from the iterating value’s seqIterator() method. The loop then continues to invoke nextSeq() from the SequenceIterator to get the next sequence object until none is returned. The list object produces an index and value tuple when iterated like this.

-- Iterate on values and indexes.
for list each [i, n]:
    print '{i} -> {n}'

-- Iterate on just indexes.
for list each [i, _]:
    print i 

The for clause can also iterate over maps.

var map = { a: 123, b: 234 }

-- Iterate on values.
for map each v:
    print v

-- Iterate on values and keys.
for map each [k, v]:
    print '{k} -> {v}'

-- Iterate on just keys.
for map each [k, _]:
    print k

You can exit a loop using break.

for 0..10 each i:
    if i == 4:
    print i
-- This loop stops printing once `i` reaches 4.

You can skip the rest of the loop and go to the next iteration using continue.

for 0..10 each i:
    if i == 4:
    print i
-- This loop prints 0 through 9 but skips 4.

Matching. #

Matching is similar to a switch statement. The expression to the right of match is evaluated and execution jumps to the declared case with the matching value. Multiple cases can be grouped together using a comma separator. An optional else fallback case is executed when no other cases were matched.

Incomplete: Not all types can be used in the case clause such as ranges.

var val = 1000
match val:
    0..100: print 'at or between 0 and 99'
    100: print 'val is 100'
        print 'val is 200'
    300, 400:
        print 'combined case'
        print 'val is {val}'

Try/Catch. #

The try catch statement, try else and try expressions provide a way to catch a throwing error and resume execution in a different branch. Learn more about Error Handling.

Deferred Execution. #

Planned Feature