The flow-control statements of LPC determine the order in which the code will be executed. There are several different ways of doing this. Some differ just in how the code look, while other have an impact on performance.
A statement is an expression followed by a semi-colon, ';'. Curly braces, '{' and '}' are used to group statements into blocks, which are also known as compound statements. Syntactically, a block is equivalent to a single statement.
The if-else statement is used to allow one expression to determine what statements should be executed. It has the form
if (expr) statement1 else statement2with the 'else' part being optional. If the value of 'expr' is true (i.e. non-zero), statement1 will be executed, else statement2 will be executed (if the else-branch exists).
The else-branch, when it exists, is associated with the latest else-less if. If that is not what you want, you need to use '{' and '}' to force the correct association.
A rather common construction is to let the optional 'else' part of an if-else statement be another if-else statement. One then arrives at something like
if (expr1) statement1 else if (expr2) statement2 else if ( ... ) ... else statementNThis chain is terminated as soon as an expression is true and the corresponding statement has been executed. This is a more general solution than the 'switch' statement discussed further on.
In LPC, a switch-statement is a multi-branch that tests whether an expression matches one of a set of constant integers or strings.
switch(expr) { case constant1 : statement1 case constant2 : statement2 ... case constantN : statementN default: statementD }The statement after the constant matching 'expr' is executed. If there is no match, the statement following the 'default' label is executed. The default part is optional.
Since the matching only determines the branching, all statements following the chosen branch at 'case constantI' will be executed, unless 'statementI' contains a 'break' statement. In a function, a 'return' statement will work nicely, too.
The 'constantI' can use the range operator to allow for a whole range of values. For example:
switch(random(10)) { case 0 .. 8 : return 1; default : return 0; }
The while loop is the basic loop in LPC. It is simply expressed as
while (expr) statementwhere 'expr' is evaluated, and if found true 'statement' is executed. Then 'expr' is evaluated again, etc., until it yields zero, at which point the program after 'statement' is executed.
for (expr1; expr2; expr3) statementis essentially equal to
expr1; while (expr2) { statement expr3; }All three of 'expr1', 'expr2' and 'expr3' are optional, but if 'expr2' is skipped, it is assumed to be true all of the time. The loop then goes on forever8 unless terminated by other means.
Both the while and for loops test the expression at the top of the loop. The do-while loop on the other hand, does the test at the end of the loop:
do statement while (expr);In LPC, this is actually faster9 than while and for loops.
The foreach loop is the fastest of them all. It works on an array:
foreach(var, array) statementThe variable 'var' is set to each of the elements of 'array' in turn, and 'statement' is executed.
There are two statements that can be used to alter the flow of a loop: break and continue. The continue statement makes the loop start over again at the top, while break exits the innermost enclosing loop (or switch) immediately.