'For' And 'While' Loops |
Previous Next |
IntroductionLoops are used in programming languages to repeat tasks. For example, every pixel should be set to a green color. Loops run as often as a given condition is true. MADRIX Script offers two possible forms of loops, the for-loop and the while-loop. Both are similar to the loops in the programming language C.
'While'-LoopThe while-loop is built in this way: while(condition) The statement may be a single expression statement. If more than one statement needs to be executed, a block is needed, as shown in the following example: int x = 0; 'For'-LoopThe for-loop is built like this: for(initialization; condition; expression) This is the same as: initialization; The initialization-part may contain the declaration of a new variable or an expression. It is executed the first time the loop runs. There may be different initializations separated by comma. Newly declared variables only exist within the for-loop. After initialization, the condition is checked. If it is unequal to 0, the given statement is executed. (In this respect, a value of 0 represents false. But a while-loop will only be executed if the condition is true. And true is represented by a value of 1, which in turn is unequal to 0.) The condition-part contains an expression. As long as the given expression is not 0 or false, the given statement is executed. If the condition-part is empty as discussed beneath, it will be interpreted as true. The expression in the expression-part is executed each time after running the statement and before checking the condition. There may be different expressions separated by comma. It is possible to leave different parts of the for-loop empty. But semicolons are necessary, nevertheless. This may be used to implement the initialization outside the loop. Here are three examples for for-loops. The first one is an endless loop: for( ; ;)
Controlling Loops: 'Break' And 'Continue'There are two possibilities to control a loop. First, it is possible to interrupt the execution of a loop (break). Furthermore, there is a way to skip the rest of the statements of the loop and to go to its beginning (continue). 'Break'With the keyword break a loop can be quit immediately. For example: int x = 0; The execution of the script is continued after the loop. No other statement within the loop is executed after break.
'Continue'With the keyword continue it is possible to skip the rest of the statements within a loop and to start anew. For example: int x = 0; Examples'While' LoopThe following example for the MAS Script Effect draws parts of a cosinus curve onto the matrix while changing the background color. void InitEffect() 'For' LoopHere is another full example for the MAS Script Effect which uses fields to store random colors and to fill the matrix with them. persistent color g_MatrixColors[][]; |
MADRIX Version: 5.7 | Script Version: 3.23 |
[Ctrl & +/-] = Zoom In/Out | [Ctrl & 0] = 100% |
Previous
Next
|
Enable automatic translation | Activer la traduction automatique | |