Reflex Flow Control - RapturePlatform/Rapture GitHub Wiki
Flow Control
Reflex has the standard flow control statements such as if ... else
, while
, for loops
.
If
The Reflex if
statement has the following form:
if booleanExpression do
block
else do
block
end
For statements without an else block the complete else do block
can be omitted.
// An If statement
a = 4;
b = 2;
if a > 3 do
println("A is greater than 3");
else do
println("A is less than or equal to 3");
end
if b == 2 do
println("Yes, b is 2");
end
Note that there does not need to be a semi-colon after the end
keyword here.
While
The Reflex while
statement has the following form:
while booleanExpression do
block
end
And an example:
// A while loop
a = true;
b = 0;
while a do
b = b + 1;
if b > 5 do
a = false;
end
end
For
Reflex has two different for
loop forms. The first, the counting form, assigns a numeric variable the values from a starting number to an ending number (inclusive) and calls the inner block for each iteration.
// A for loop
for a = 0 to 10 do
println("The value of a is " + a);
end
The second is known as the iterator form, and it takes as a secondary argument a list expression (which can be a variable or an expression that yields a list). The value of the variable is set to each element in the list and the inner block is called with that element set.
// A for loop
a = [1, 2, 3, 4 ];
b = [];
for c in a do
b = b + ( c * 2 );
end
assert(c == [2, 4, 6, 8 ];
Break and Continue
Reflex also supports break
and continue
semantics, which work as expected. The following code snippets (which assert correctly) show this behavior.
res = [];
for i = 0 to 10 do
res = res + i;
if i == 5 do
break;
end
end
assert(res == [0,1,2,3,4,5]);
and a continue example:
res = [];
for i = 0 to 10 do
if i < 5 do
continue;
end
res = res + i;
end
assert(res == [5,6,7,8,9,10]);