Evolve has a very small command set and therefore only has 3 commands for flow control.
Each command can take multiple operators the order the logic statements are processed in is from left to right.
A < 10 || c > e && f == r
Is the equivalent to
((A < 10 || c > e) && (f == r))
The if statement allows the program to make decisions, it can either be used on its own with the _if command or it can also be used with the else command.
The the following code shows it in action, it waits for an input and then tells the user if it was a space or number.
//---------------------------------------
// File: if.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
//use a byte input
ByteInput = 0x00
//wait until a key is pressed
key_inwait ByteInput
//tell the user if it was a number
if ByteInput >= '0' && ByteInput <= '9'
outl "Number"
else
//tell the user if it was a space
if ByteInput == ' '
outl "Space"
else
outl "Not a number or space"
_if
_if
_block
The loop command allows programs to loop a specific number of times. It the specified variable gets set to zero on the first pass and then gets incremented.
//---------------------------------------
// File: Loop.e
//---------------------------------------
//---------------------------------------
// Block: Go
//---------------------------------------
block Go
loop X 10
out X ") "
loop Y X
out Y " "
_loop
new_line
_loop
_block
The above code shows two loops with the value of the first loop being used to determine the number of times the second loop is called.
The Iter command allows programs to iterate through variable lists. The first argument is a variable which will contain the current value from the list and the second argument is the list to iterate through. While iterating through the list an additional variable is automatically created giving the index of the current iteration in the list. This variable has the same name as the first argument with an 'I' appended to it
//---------------------------------------
// File: ILoop.e
//---------------------------------------
//---------------------------------------
// Block: Go
//---------------------------------------
block Go
//int iterator
Test1 = { 11 22 33 54 66 77 }
iter Value1 Test1
outl Value1I " " Value1
_iter
new_line
//str iter
Test2 = { "AA" "BB" "CC" "DD" "EE" "FF" }
iter Value2 Test2
outl Value2I " " Value2
_iter
new_line
//nested
iter Value2 Test2
iter Value1 Test1
outl Value1 " " Value2
_iter
new_line
_iter
_block
The above code shows two loops with the value of the first loop being used to determine the number of times the second loop is called.
The while statement enables you to create loops in the code the end of the loop is indicated by the _while command.
//---------------------------------------
// File: while.e
//---------------------------------------
//---------------------------------------
// Block: KeyInput
//---------------------------------------
block KeyInput
outl "press 'x'"
//use a byte input
ByteInput = 0x00
while ByteInput != 'x'
//wait until a key is pressed
key_inwait ByteInput
_while
_block
//---------------------------------------
// Block: Count
//---------------------------------------
block Count
//use a byte input
I = 0
while I <= 10
outl I
++ I
_while
_block
//---------------------------------------
// Block: go
//---------------------------------------
block Go
//call the key input
KeyInput
//call the count
Count
_block
The switch statement is useful when there is a single variable to be checked against multiple values. The variable to be checked can be any type and the values that it can be checked against unlike other programming languages do not need to be constants .
The other difference compared to other languages that have a switch type statement is that each case calls a block, it is not possible to put additional code into the switch statement.
If none of the options match the required value then there is a default case which will be used if it is defined.
The first line of a switch command specifies the variable to check and the last line of a switch command is the _switch.
Between the switch and _switch command are the case commands each case line consists of the value to check and then the block to call followed by any parameters for the block.
The following code gives a demonstration of the switch statement in use.
//---------------------------------------
// File: switch.e
//---------------------------------------
//---------------------------------------
// Block: Option1
//---------------------------------------
block Option1
outl "option1"
_block
//---------------------------------------
// Block: Option2
//---------------------------------------
// Variables:
// Str
//---------------------------------------
block Option2 Str
outl "option2 " Str
_block
//---------------------------------------
// Block: OptionDefault
//---------------------------------------
block OptionDefault
I = 0
while I < 3
switch I
0 Option1
1 Option2 I
2 Option2 I
_switch
++ I
_while
_block
//---------------------------------------
// Block: go
//---------------------------------------
block Go
VariableKey = 0
outl "Press 'x' to exit"
//loop unitl x is pressed
while VariableKey != 'x'
//wait for a key input
key_inwait VariableKey
//switch on the input value
switch VariableKey
'1' Option1
'2' Option2 VariableKey
default OptionDefault
_switch
_while
_block
The conditional operator '?' allows a value to be assigned to a variable depending on the result of a comparison. The following example shows it in action
//---------------------------------------
// File: ConditonalOperator.e
//---------------------------------------
//---------------------------------------
// Block: Go
//---------------------------------------
block Go
I = 0
while I < 10
? I < 5 Str = "<5" : ">5"
outl Str
++ I
_while
_block
The conditional operator starts with a '?' followed by a simple comparison only one comparison is allowed this is followed by the variable to assign the result to and then to values the first is assigned if the comparison evaluates to true other wise the second value is assigned