- This wiki is out of date, use the continuation of this wiki instead
Loops
From FenixWiki
Revision as of 15:16, 1 May 2007 (edit) FCR (Talk | contribs) ← Previous diff |
Current revision (19:03, 29 February 2008) (edit) (undo) 85.144.194.29 (Talk) (→Manipulating a loop - Grammar error.) |
||
(10 intermediate revisions not shown.) | |||
Line 1: | Line 1: | ||
- | [[category: | + | [[category:general]] |
- | + | ||
- | == | + | == Loops == |
- | '''LOOP''' | + | '''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 === | |
- | + | <pre> | |
- | + | Loop | |
- | + | // Statements | |
- | + | End | |
+ | </pre> | ||
+ | The statements will be repeated indefinitely. | ||
+ | |||
+ | === While ... End === | ||
+ | <pre> | ||
+ | While(<condition>) | ||
+ | // Statements | ||
+ | End | ||
+ | </pre> | ||
+ | The statements will be repeated while the condition is fulfilled. | ||
+ | |||
+ | === Repeat ... Until === | ||
+ | <pre> | ||
+ | Repeat | ||
+ | // Statements | ||
+ | Until(<condition>) | ||
+ | </pre> | ||
+ | The statements will be repeated until the condition is fulfilled. | ||
+ | |||
+ | === For ... End === | ||
+ | <pre> | ||
+ | For(<initializer>;<condition>;<counting expression>) | ||
+ | // Statements | ||
+ | End | ||
+ | </pre> | ||
+ | First, the initializer will be executed.<br /> | ||
+ | Then the statements will be repeated until the condition is fulfilled.<br /> | ||
+ | After each run of the statements, the counting expression is executed. | ||
+ | |||
+ | === From ... End === | ||
+ | <pre> | ||
+ | From <variable>=<startvalue> To <endvalue> Step <incrementvalue> | ||
+ | // Statements | ||
+ | End | ||
+ | </pre> | ||
+ | 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. Note [[Fenix]] [[0.85]] and before have a bug: the count- or stepping statement does not get executed before jumping to the checking of the condition. | ||
+ | |||
+ | 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 == | == Example == | ||
<pre> | <pre> | ||
- | + | Program loops; | |
+ | Const | ||
+ | startvalue = 1; | ||
+ | endvalue = 8; | ||
+ | incrementvalue = 2; | ||
+ | End | ||
+ | Private | ||
+ | int c; | ||
+ | End | ||
Begin | 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 | End | ||
</pre> | </pre> | ||
+ | Used in example: [[say]](), [[constants]] | ||
+ | |||
+ | In the example, all the loops do the same thing, illustrated by the use of the same constants. |
Current revision
Contents |
[edit] 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:
[edit] Loop ... End
Loop // Statements End
The statements will be repeated indefinitely.
[edit] While ... End
While(<condition>) // Statements End
The statements will be repeated while the condition is fulfilled.
[edit] Repeat ... Until
Repeat // Statements Until(<condition>)
The statements will be repeated until the condition is fulfilled.
[edit] 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.
[edit] 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.
[edit] 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. Note Fenix 0.85 and before have a bug: the count- or stepping statement does not get executed before jumping to the checking of the condition.
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.
[edit] 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.