Colobot Wiki
Advertisement

The instruction while () {} is used to repeat a set of instructions several times.

Basic use[]

The most frequent use of while consists in repeating a set of instructions again and again. In order to achieve this, write while (true) {} and put the instructions to be repeated in braces {}. As an example, here is a program that repeats again and again the following actions:

	while (true)
	{
		item = radar(AlienSpider);
		turn(direction(item.position));
		fire(1);
	}

Just execute this program once, and it will kill all spiders around it.

For specialists[]

Syntax :

while ( condition )
{
	Instructions ...
}

This instruction allows you to perform the instructions inside the block several times.

Be careful not to confuse the instruction while( ) { } with the instruction do { } while( ); as the latter tests the condition only after the instructions in the block have been performed a first time.

condition[]

The instructions in the block are performed over and over again, as long as the condition is true. 

Here is an example :

int i = 0;
while ( i < 10 )
{
	Instructions ...
	i = i+1;
}

Attention[]

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

The instructions break and continue can be useful inside a block following the instruction while { }.

Advertisement