|5||for||range controlled repetition|
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.
This is a statement with conditional execution augment.
statement if boolean_variable; statement if (boolean_expression);
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);
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.
# single path decision if expression then ** branch statements ... end if;
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.
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;
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;
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.
#nested selector if expression then ** direct path ... if expression then ** nested path ... end if; end if;
In EVE you can use keywords "and if" to create a cascade decision:
cascade of decision
#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.
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.
#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.
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.
#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.
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.
while loop diagram
#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 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;
"c","d","e" i = 5
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 diagram
#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;
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;
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