|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles | Qt Script for Applications |

[Prev: else] [Home] [Next: if]

### for

    for ( i = 0; i < limit; i++ ) {
        Statements;
    }

This keyword is used to create a loop that executes a fixed number of times.

The `for` statement is broken into parts as follows: the keyword `for`, an opening parentheses, zero or more statements (the first part), a semi-colon, a conditional expression (the second part), a semi-colon, zero or more statements (the third part), a closing parentheses, and finally a statement or block that is governed by the `for` loop.

The first part of the `for` statement is typically used to initialize (and possibly declare) the variable used in the conditional in the second part of the `for` loop statement. This part is executed once before the loop begins. This part may be empty.

The second part contains a conditional expression. The expression is evaluated before each iteration of the loop (including before the first iteration). If this expression evaluates to `false`, the statement or block governed by the `for` loop is not executed and control passes to the statement that follows. If the condition is never `true`, the statement or block governed by the `for` loop will never be executed. If the condition expression is `true`, the statement or block governed by the `for` loop is executed, and then the third part of the `for` statement is executed, before control is passed back to the conditional expression with the whole process being repeated. This part should not be empty.

The third part contains statements which must be executed at the end of every iteration of the loop. This part is typically used to increment a variable that was initialized in the first part, and whose value is tested in the second part. This part may be empty.

Example:

        var a = [ "a", "b", "c", "d", "e" ];
        for( var i = 0; i < a.length; i++ ) {
            System.println( a[ i ] );
        }

See also do, while, continue and break.

[Prev: else] [Home] [Next: if]