index

Statements

Statement blocks

{
   STATEMENTS
}

A statement block is a collection of statements.

Variable declarations

TYPE POINTER VarName = EXPR, POINTER VarName2 = EXPR;

TYPE is exchanged for the data type. POINTER can be zero or more stars, *, showing the pointer level, e.g. one star, *, means a pointer to a TYPE, two stars, **, means a pointer to a pointer to a TYPE. The EXPR expression must evaluate to the same data type, and pointer level. The initialization expressions are optional. Any number of variables can be declared on the same line separated with commas.

Variables can be declared as const. In these cases the value of the variable cannot be changed after initialization.

Variables not initialized cannot be assumed to be set to zero, although object variables usually are since the engine's memory management depends on it.

Variables must be declared before they are used within the statement block, or any sub blocks. When the code exits the statement block where the variable was declared the variable is no longer valid.

Conditions: if / if-else / switch-case

if( BOOL_EXP ) STATEMENT

if( BOOL_EXP ) STATEMENT else STATEMENT

BOOL_EXP can be exchanged for any expression that evaluates to a boolean data type. STATEMENT is either a one-line statement or a statement block.

switch( INT_EXP )
{
case INT_CONST:
  STATEMENT

default:
  STATEMENT
}

If you have an integer (signed or unsigned) expression that have many different outcomes that should lead to different code, a switch case is often the best choice for implementing the condition. It is much faster than a series of ifs, especially if all of the case values are close in numbers.

Each case should be terminated with a break statement unless you want the code to continue with the next case.

The case value can be a const variable that was initialized with a constant expression. If the constant variable was initialized with an expression that cannot be determined at compile time, it cannot be used in the case values.

Loops: while / do-while / for

while( BOOL_EXP ) STATEMENT

do STATEMENT while( BOOL_EXP );

for( INIT ; BOOL_EXP ; NEXT ) STATEMENT

BOOL_EXP can be exchanged for any expression that evaluates to a boolean data type. STATEMENT is either a one-line statement or a statement block. INIT can be a variable declaration, an expression, or even blank. If it is a declaration the variable goes out of scope as the for loop ends. NEXT is an expression that is executed after STATEMENT and before BOOL_EXP. It can also be blank. If BOOL_EXP for the for loop evaluates to true or is blank the loop continues.

Loop control: break / continue

break;

continue;

break terminates the smallest enclosing loop statement. continue jumps to the next iteration of the smallest enclosing loop statement.

The break and continue statements are only valid inside a loop statement.

Return statement

return EXPR;

Any function with a return type other than void must be finished with a return statement where EXPR is an expression that evaluates to the same data type as the function. Functions declared as void can have return statements without any expression to terminate early.

Expression statement

EXPR;

Any expression may be placed alone on a line as a statement. This will normally be used for variable assignments or function calls that don't return any value of importance.

top