Colobot Wiki
Advertisement

Syntax:

for ( before ; condition ; end )
{
	Instructions ...
}

This instruction allows you to execute the instructions contained in the block a certain number of times.

before[]

This set of instructions is executed before the first loop instance.

condition[]

This condition determines if another instance of the loop must be executed. It is tested before every instance of the loop. 

end[]

This set of instructions is executed at the end of every instance of the loop.

Example: count from 1 to 4

for ( i = 1 ; i <= 4 ; i++ )
{
	message(i) ;
}

The following example is strictly equivalent to a for-loop, but it uses the instruction while:

before;
while ( condition )
{
	Instructions ...
	end;
}

Attention[]

Do not put a semicolon at the end of the line for ( ).

The instructions break and continue can be useful inside a block following the instruction for.

Executing more instructions[]

In the before and end part of a for loop you can specify more than one instruction by using comma.

Example:

int i = 0;
int j;
for (i++, j = 2; i < 3 && j > 0; i++, j--)
{
message(i);
message(j);
}

The output of the above code is 1 2 2 1.

Advertisement