'For' & 'While' Loops

   Print  Previous  Next

Introduction

Loops 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'-Loop

The while-loop is built in this way:

while(condition)

    statement

 

The statement may be a single expression statement. If more then one statement needs to be executed, a block is needed, as shown in the following example:

int x = 0;

while(x < 10)

{

    SetPixel(WHITE, x, 0);

    i++;

}

 

'For'-Loop

The for-loop is built like this:

for(initialization; condition; expression)

    statement

 

This is the same as:

initialization;

while(condition)

{

    statement;

    expression;

}

 

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 before running the statement and before checking the condition. But not the first time. 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( ; ;)

{

    do anything;

}

 

 

for(int x = 0; x < GetMatrixWidth(); x++)

    for(int y = 0; y < GetMatrixHeight(); y++)

        SetPixel(WHITE, x, y);

 

 

for(int x = 0; x < GetMatrixWidth(); x++)

{

    if(x % 2)

        DrawPixelLine(WHITE, x, 0, x, GetMatrixHeight());

    else

        DrawPixelLine(BLACK, x, 0, x, GetMatrixHeight());

}

 

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;

while(x < 10)

{

    if(i >= GetMatrixWidth())

        break; //leave loop now!

 

    SetPixel(WHITE, x, 0);

    x++;

}

 

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;

while(x < 10)

{

    if(x % 2 == 0)

        continue;

    SetPixel(WHITE, x++, 0);

}

 

Examples

'While' Loop

The following example "cos" draws parts of a cosinus curve onto the matrix while changing the background color.

void InitEffect()

{

    SetSpeed(1);

}

 

void RenderEffect()

{

    color col = {200, 200, 100, 200};

    color colBK={random(0,150), random(0,255), random(0,100), random(0,255)};

    //set background

    Clear(colBK);

    //draw cosine curve

    int px = 0;

    int py = 0;

    float y;

    float t = 0;

    while(px < GetMatrixWidth())

    {

        y = cos(t) * GetMatrixHeight();

        py = (int)y;

        t = t + (PI * 3 / GetMatrixHeight());

        px++;

        SetPixel(col, px, py);

    }//while[x < GetMatrixWidth()]

}

 

 

'For' Loop

Here is another full example which uses fields to store random colors and to fill the matrix with them.

persistent color g_MatrixColors[][];

void RenderEffect()

{

    //select random color

    color col = {random(0,255), random(0,255), random(0,255), random(0,255)};

 

    //select random pixel coordinates

    int px = random(0,GetMatrixWidth()-1);

    int py = random(0,GetMatrixHeight()-1);

 

    //save the selected color

    g_MatrixColors[px][py] = col;

 

    //draw points of the field

    for(px = 0; px < GetMatrixWidth(); px++)

    {

        for(py = 0; py < GetMatrixHeight(); py++)

        {

            SetPixel(g_MatrixColors[px][py], px, py);

        }//for[each line]

    }//for[each column]

}

 

void InitEffect()

{

    for(int x = 0; x < GetMatrixWidth(); x++)

        for(int y = 0; y < GetMatrixHeight(); y++)

        {

            g_MatrixColors[x][y].r = 0;

            g_MatrixColors[x][y].g = 0;

            g_MatrixColors[x][y].b = 0;

            g_MatrixColors[x][y].w = 0;

        }

        SetSpeed(5);

}