Colobot Wiki
Advertisement
You can use braces { } to group a number of instructions together in order to make one single block out of them, for example:

{

		float  t;
		t = a;
		a = b;
		b = t;  // switches round a and b
	}

Every instruction in the block is followed by a semicolon, but the block itself is not. 

Let us take the following example with the instruction if to illustrate the use of blocks in a program:

	if ( a < b )
		instruction 1;
		instruction 2;
	instruction 3;

If the condition is true, instructions 1, 2 and 3 are performed. If the condition is false, only instruction 1 is ignored, instructions 2 and 3 are performed. The fact that instruction 2 is lined up with instruction 1 does not matter.

If you want to perform instructions 1 and 2 only if the condition is true, you have to bracket them together in a block: 

	if ( a < b )
	{
		instruction 1;
		instruction 2;
	}
	instruction 3;

Blocks may be needed with instructions if, while and for, in order to group several instructions that should be performed only if a condition is true, or repeated several times.

You can fit blocks into other blocks, on as many levels as you need. Here is an example of imbrication on two levels :

	if ( a > b )
	{
		int  i = 0;
		while ( i < 18 )
		{
			move(10);
			turn(5);
			i = i+1;
		}
	}

You had better line up the open brace { with the closing brace } in order to improve readability, but it is not compulsory. The following example takes less space, is equivalent to the previous example, but it is not advisable to write your programs in the following style :

	if(a>b) { int i=0; while(i<18) {
	move(10);turn(5);i=i+1; }}
Advertisement