Toggle menu
862
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 07:15, 14 December 2011 by Cg_pham (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)



The For Loop

The general form of the for loop for repeating a single statement is:

for (initialization; condition; iteration) statement;

For repeating a block, the general form is:

for (initialization; condition; iteration)

{

Statement sequence

}

The initialization is usually an assignment statement that sets the initial value of the loop control variable, which acts as the counter that controls the loop.

The condition is an expression of type bool that determines whether the loop will repeat.

The iteration expression defines the amount by which the loop control variable will change each time the loop is repeated.

The for loop will continue to execute as long as the condition tests true. Once the condition becomes false, the loop will exit, and the script execution will resume on the statement following the for.

For example, the following script prints the numbers from 1 to 5 in increments of 1:

{

for(number i = 1; i<6; i=i+1)

{

print(i);

}

print("Counting completed.");

}

The output from the script is shown here:

1

2

3

4

5

Counting completed.