Sage-Code Laboratory
index<--

EVE Control Flow

Most languages have similar flow control statements. These are block statements that can execute or no depending on one or more conditions. A control flow statement can be a decision statement a repetitive statement or a selection statement.

Bookmarks

1if conditional execution
2tehn conditional branch
3else alternative branch
4loop unconditional repetition
5while conditional repetition
6for range controlled repetition

if

Keyword "if" establish a conditional statement. Also it can be used in expressions. A conditional statement can be a simple statement or a block statement. You can use "if" after a simple statement or before a block statement.

Pattern:

This is syntax for a simple statement with conditional execution.

statement if boolean_variable;
statement if (boolean_expression);

Examples:

Next examples show how we can use conditional to print someting when expression is fulfelled.

#conditional print
print "a is even" if (a % 2 = 0);
print "a is odd"  if (a % 2 != 0);

then

We use "if-then" branching statement like most imperative languages, to make a decision statement. This is used to start a block that is executed or no depending on a condition. The condition must be a logical value or expression. Eve do not support other types for making decision statements.

Pattern:

# single path decision
if expression then
  ** branch statements
  ...
end if;

else

You can use "else" keyword to create alternative secondary path for a decision statement. The second path is executed only if the conditional expression is false. This enable you to create two alternative blocks of code.

decision

decision diagram

Pattern 1:

In next pattern we use "if-then-else" keywords to create a dual path decision statement.

# dual path decision
if expression then
  ** true path
  ...
else
  ** false path
  ...
end if;

Pattern 2:

In other languages we use ternary operator "?" but in EVE we can use "if" and "else" keywords to create complex conditional expressions like these below:

#conditional expressions
v := value1 if condition1 else vlue2;
v := value1 if condition1 else vlue2 if condition2 else value3;

Nested decisions

You can use two conditions one inside of another. This is called nested decision statement. Of course both statements can have "else" path, so with 1 nested "if" you can have in total 4 possible branches. Imagine if you nest on 3 levels, the logic can become very complex.

Pattern 1:

#nested selector 
if condition_1 then
    ** first branch
    ...
    if condition_2 then
        ** second branch
        ...
    else 
        ** third branch
        ....
    end if;        
else if condition_3 then
    ** third branch
    ...
else 
    ** forth branch
    ....
end if;        

Notes: Previous pattern show a complex situation having 3 decision statements. Observe that we can have only 2 end-if's. Woaw! That is because EVE is compiler is smart enaugh to detect the indentation change and will close both clocks. Ehe "else" block do not need "end-if".

Pattern 2:

#nested selector 
if condition_1 then
    ** first branch
    ...
else 
    if condition_3 then
        ** third branch
        ...
    else 
        ** forth branch
        ...
    end if;
    ** more code here
    ...     
end if;        

Notes: Previous pattern show a complex situation having 2 decision statements. Observe second statement is nested but else block contains more code. So we use 2 end-if's. This is how cool EVE compiler is! It uses indentation to tell else block is not yet finished.

loop

Execute a block of code until a "break" statement is encounter. The breack condition is used with "if" conditional and is usually located before the end of the loop block. You can use "repeat" statement with a "if" conditional to restart the loop.

loop

infinite loop

Syntax:

#infinite loop
[label:] loop
    ** shortcut iteration
    repeat [label] if condition ;
    ...
    ** early intreruption
    break [label] if condition;
    ...
end loop [label];

Notes:

  • Repeat is equivalent to "continue" statement in other languages,
  • The label is optional, that's why is in square brackets.
  • The label is used, for nested loops otherwise is not necessary.
  • while loop

    Execute a block of code as long as one condition is true. When the condition become false the repetitive block stops and block "else" is executed. If the condition is never true only the "else" block is executed.

    whle-loop

    while loop

    Syntax:

    #conditional loop
    while condition loop
        ** shortcut iteration
        if condition then 
            repeat;
        end if;    
        ...
        ** early interruption
        if condition then 
            break;
        end if;    
        ...
    else
        ** alternative path
        ...
    end loop;
    

    Example:

    ** example of collection iteration
    driver while_demo:
    locals
        List this := ["a","b","c","d","e"];
        Integer i := 0;
        Symbol  e;
    process
        while i < this.length() loop
            e := this[i];
            i := i + 1;
            if e  >= "c" then
                write e; 
                if e is not this.tail then
                   write ','; 
                end if;   
            end if;
        else
            write ('i = ' + i);
        end loop;
        print;
    return;
    

    Output:

    "c","d","e"
    i = 5
    

    for loop

    This statement use one control variables to visit elements in a collection or domain. First use-case of this is to iterate over a range of numbers. In EVE the range is represented as (min..nax).

    for-loop

    for ... loop

    Pattern:

    #for loop syntax
    for var in (min..max) loop
        ** block statements;
        ...
        ** next iteration
        if condition then 
            repeat;
        end if;   
        ...
        ** early interruption
        if condition then 
            break;
        end if;   
        ...
    end loop;
    

    Notes:

    Example of range iteration using step ratio 2:

    #example of range iteration
    for i in (1..10:2) loop
        ** write only odd numbers
        write i;
        write ',' if (i < 9);
    end loop;
    print;
    

    Output:

    1,3,5,7,9
    

    output


    Read next: Classes