[Next] [Up] [Previous] [Contents] [Index]
Next: 5. Functions and Program Up: NannyMUD LPC Previous: 3. Types, operators and


4. Flow Control

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.

4.1 Statements and Blocks

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.

4.2 Conditional execution: If-Else

The if-else statement is used to allow one expression to determine what statements should be executed. It has the form

  if (expr)
with 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.

4.3 Conditional execution: Else-If

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)
  else if (expr2)
  else if ( ... )
This 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.

4.4 Conditional execution: Switch

In LPC, a switch-statement is a multi-branch that tests whether an expression matches one of a set of constant integers or strings.

    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:

    case 0 .. 8 :
      return 1;
    default :
      return 0;

4.5 Loops: While

The while loop is the basic loop in LPC. It is simply expressed as

  while (expr)
where '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.

4.6 Loops: For

The for loop

  for (expr1; expr2; expr3)
is essentially equal to
  while (expr2)
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.

4.7 Loops: Do-While

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:

  while (expr);
In LPC, this is actually faster9 than while and for loops.

4.8 Loops: Foreach

The foreach loop is the fastest of them all. It works on an array:

  foreach(var, array)
The variable 'var' is set to each of the elements of 'array' in turn, and 'statement' is executed.

4.9 Break and Continue

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.


... forever8
'Forever' is not really true, 'until max eval cost' would be a better description. That feels like eternity anyway.
... faster9
At least on the driver used when this was written, the infamous 1.15.3.

[Next] [Up] [Previous] [Contents] [Index]
Next: 5. Functions and Program Up: NannyMUD LPC Previous: 3. Types, operators and
Mats Henrik Carlberg