- This wiki is out of date, use the continuation of this wiki instead
Loops
From FenixWiki
Contents |
Loops
LOOP-END, WHILE-END, REPEAT-UNTIL, FOR-END, FROM-END
Loops are used to create iterations in your code. The statements between these words will get repeated depending on a condition. There are several types of loops:
Loop ... End
Loop // Statements End
The statements will be repeated indefinitely.
While ... End
While(<condition>) // Statements End
The statements will be repeated while the condition is fulfilled.
Repeat ... Until
Repeat // Statements Until(<condition>)
The statements will be repeated until the condition is fulfilled.
For ... End
For(<initializer>;<condition>;<counting expression>) // Statements End
First, the initializer will be executed.
Then the statements will be repeated until the condition is fulfilled.
After each run of the statements, the counting expression is executed.
From ... End
From <variable>=<startvalue> To <endvalue> Step <incrementvalue> // Statements End
First, the startvalue will be assigned to the variable. Then the statements will be repeated and the incrementvalue added to the variable. When the variable is greater than the endvalue, the loop ends. Note that the incrementvalue has to be a constant. The Step <incrementvalue> is optional.
Manipulating a loop
There are more ways to manipulate a loop, both internally and externally.
If the code inside a loop reaches a break; statement, the loop is immediately ended. This is particularly useful in the Loop-End loop, because that one has no other way to end the loop and continue code beneath it.
If the code inside a loop reaches a continue; statement, it executes the possible count- or stepping statement and then continues to the checking of the condition. So in a Repeat-Until loop, it would just skip to the Until() part.
If the loop is run in a specific process, one can influence the execution of the loop, by changing the state of that process, by use of signal() and signals.
Example
Program loops; Const startvalue = 1; endvalue = 8; incrementvalue = 2; End Private int c; End Begin // LOOP c = startvalue; Loop say("Loop: " + c); c+=incrementvalue; if(c>endvalue) break; end End say("End Loop: " + c); // WHILE c = startvalue; While(c<endvalue) say("While: " + c); c+=incrementvalue; End say("End While: " + c); // REPEAT c = startvalue; Repeat say("Repeat: " + c); c+=incrementvalue; Until(c>endvalue) say("End Repeat: " + c); // FOR For(c=startvalue;c<endvalue;c+=incrementvalue) say("For: " + c); End say("End For: " + c); // FROM From c=startvalue To endvalue Step incrementvalue say("From: " + c); End say("End From: " + c); Loop frame; End End
Used in example: say(), constants
In the example, all the loops do the same thing, illustrated by the use of the same constants.