Sage-Code Laboratory
index<--

EVE Control Flow

Most languages have similar flow control statements. These are block statements that can execute or not depending on specific conditions. A control flos statement can be a decision statement a repetitive statement or a selection statement. EVE do not have a selection statement but it has repetition and decision statements.

Bookmarks

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

if

Keyword "if" establish a conditional for a statement. Also it can be used in expressions. A conditional can suppress execution for a particular statement that is not a block statement.

Pattern:

This is a statement with conditional execution augment.

statement if boolean_variable;
statement if (boolean_expression);

Examples:

Next examples show how we can use conditional to print someting when expression is fulfelled. Notice we can not use "else" with conditional augments.

#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 do, to make a decision statement. This statement is used to start a block that is executed or not depending on expression value.

Pattern:

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

else

You can use "else" keyword to create secondary path into 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:

#nested selector 
if expression then
    ** direct path
    ...
    if expression then
    ** nested path
    ...
    end if;
end if;

Cascade

In EVE you can use keywords "and if" to create a cascade decision:

cascade decision

cascade of decision

Pattern:

#cascade decidion
if expression then
    ** direct path
    ...
and if expression then
    ** additional path
    ...
and if expression then
    ** additional path
    ...
else
    ** alternative path    
    ...    
end if;

Note: In this pattern, last path is about first condition. Only when first condition is false the "else" path will trigger. For any other "and if" expressions the decision statement is ending when the expression is false without triggering "else" block.

Note: This statement is higly experimental. We have no idea how it will work in practical applications. Until we do it remains a curiosity of EVE language. It was created for consistency with the next syntax pattern that is "or if", also known as selection ladder.

Ladder

In EVE you can use keywords "or if" to create a decision ladder. This term comes from the aspect of the diagram. A decision ladder is a convenient way to create multi-branch conditional selector.

decision

decision ladder

Pattern:

#ladder conditional
if condition_1 then
    ** first branch
    ...
or if condition_2 then
    ** second branch
    ...
or if condition_n then
    ** third branch
    ...
else
    ** default branch
    ...
end if;

Note: In this pattern, else path trigger only if all the above expressions evaluate to false. When any of the above condition is true, the "else" path will not trigger.

You can not use both "and if" and "or if" in the same decision statement. You can use though nested statements in ladders to do more complex decisions.

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.

do while

do-while diagram

Syntax:

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

Repeat is equivalent to "continue" statement in other languages.

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.

for-loop

while loop diagram

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
routine test():
    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 diagram

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 < 10);
end loop;
print;

Output:

1,3,5,7,9

Example:

In next example we itterate over a List and we print element by element starting from element "c". We ignore elements: a,b,c. We must use nested if statement to decide not to print a comma for last element.

# example of collection iteration
routine test():
    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)
            write(',') if (e is not this.tail);
        end if;
        print;
    else
        print ('i = ' + i);
    end loop;
return;

Read next: Page Name