|6||for||range controlled repetition|
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.
This is syntax for a simple statement with conditional execution.
statement if boolean_variable; statement if (boolean_expression);
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);
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.
# single path decision if expression then ** branch statements ... end if;
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.
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 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".
#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.
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.
#infinite loop [label:] loop ** shortcut iteration repeat [label] if condition ; ... ** early intreruption break [label] if condition; ... end loop [label];
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.
#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 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;
"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
#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 < 9); end loop; print;
Read next: Classes