You always need a loop structure if you want to repeat anything.
For loops we use the keyword „do“. do allows you to use two additional variables: value and index. value is the current value, index increases from zero by 1 for every step. If you don't use these variables, they will not cost any time.
Do works in several operating modes
This represents the common while-loop in programming languages like C. If you put an condition in front of the keyword do the loop will be repeated until the conditions becomes false.
i is 10 i > 0 do print i$, "\n";
For an infinite loop use
true do printf "infinite\n";
You can use the variable index to ask in which iteration you are:
true do printf index$, " iteration of infinite loop\n";
which will result into
0 iteration of infinite loop 1 iteration of infinite loop 2 iteration of infinite loop ...
The easiest way of repeating things is to count how often you want to do the given content. You simply use an unsigned integer in front of the do keyword. You can use index for the count of fulfilled iterations (starting at 0) or value for the current iteration (starting at 1).
5 do print "Index: ", index$, " Value: ", value$, "\n";
The result is
Index: 0 Value 1 Index: 1 Value 2 Index: 2 Value 3 Index: 3 Value 4 Index: 4 Value 5
Of course you can use a variable of type unsigned int:
i is 5; i do print "Index: ", index$, " Value: ", value$, "\n";
You can also use ranges to iterate.
12..15 do print "Index: ", index$, " Value: ", value$, "\n";
results into:
Index: 0 Value 12 Index: 1 Value 13 Index: 2 Value 14
Pay attention that the beginning element of the range is included, the end element of the range is excluded. So if you want to iterate from 'a' to 'z' just add 1, so 'z' is included.
'a'..'z'+1 do print "Index: ", index$, " Value: ", value$, "\n";
results into:
Index: 0 Value a Index: 1 Value b Index: 2 Value c ... Index: 23 Value x Index: 24 Value y Index: 25 Value z
Collections are everything which contains multiple elements like arrays, strings, lists.
The easiest way to do an iteration is to take an collection data type (i.e. a List or an Array) and iterate.
char word[] = "Genesys"; print "The word is: ", for( char c in word ) print c$; print "\n";
You can use an integer array to count.
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } do
print value$;
which will result in
1 2 .. 9 10
You have already seen the for-each version before. With Genesys you can also use a more c style for-loops:
for( int i:10; i > 0; i-- ) print "i: ", i, "\n";
This type of loops is used if the instructions need to be proceeded at least once. At the end of the instruction block you see the while expression deciding if the loop will be proceeded again.
int i=10; do print "i: ", i--, "\n"; while( i > 0 )
You see, that this loop will result in slightly different results. To get the same result you have to use –i instead of i–.
As in C you can break a loop with the keyword break. The following loop will count from 1 to 4 and will be broken before 5 is printed.
1..10 do
{
index == 5 then break;
print index$, "\n";
}
With continue you jump at the top of a loop, ignoring everything following:
Here the loop count from 1 to 10 but the number 5 is missing:
1..10 do
{
index == 5 then continue;
print index$, "\n";
}
If you nest a loop within another you will get trouble with index and value. They are always defined for the loop you're inside.
If you want to touch a 3×3 matrix you can use two loops:
{ 1..3 } :=
{ 1..3 } :=
print value$, "/", value$, "\n";
That won't work of course. To get the right index you have to name at least the outer loops:
x is { 1..3 } :=
y is { 1..3 } :=
print x.value$, "/", y.value$, "\n";
You do not need to give y its name. value will reference to the inner loop which is the y-loop.
This also works for 'for' and while loops.
Please note that inner.index is not equal to i and outer.index is not equal to o.
outer is for( int o : -2; o < 10; o++ )
{
inner is for( int i : -2; i < 10; i++ )
{
if( o == 1 && i == 2 ) break; // jumps after inner loop
if( o == 2 && i == 4 ) break inner; // jumps after inner loop
if( o == 3 && i == 4 ) continue; // jumps to inner loop at i++
if( o == 3 && i == 6 ) continue inner; // jumps to inner loop at i++
if( o == 4 && i == 8 ) continue outer; // jumps to outer loop at o++
if( o == 5 && i == 8 ) break outer; // jumps after outer loop
print "outer: ", outer.index$, "/", o, "; ";
print "inner: ", inner.index$, "/", i, "\n";
}
// after inner loop
}
// after outer loop
You can proceed like this with while
outer is while( true )
inner is while( true )
break inner;
or do.. while()
outer is do
inner is do
break outer;
while( true )
while( true )