Phantasmal MUD Lib for DGD
|
Phantasmal Site > DGD > DGD Reference Manual > LPC > Statements StatementsA statement is sort of a full sentence of instructions, made up
from one or more expressions. Statements usually cover no more than
a single line of code. Sometimes it's necessary to break it up
though if it becomes too long simply to improve on legibility -- a
little like avoiding long run-on sentences. For most statements you
simply break the line between two words, but if you are in the
middle of a string you need to add a backslash ( DRIVER->message("This is an example of \ a broken string.\n"); However, breaking a statement with backslash is extremely ugly
and makes the code hard to read. It's usually possible to break the
line naturally at the end of a string, or between two operators of
some kind, or even just split the string in half and add the two
parts together with the DRIVER->message("This is a better way of " + "breaking a string.\n"); Statements in LPC almost always end with The block statementThere are a lot of statements
such as conditional statements that in certain circumstances
execute only one specified statement and no more. Suppose
you want to have several statements executed and not just one?
Well, there's a special statement called block statement
that will allow you to do that. A block is defined as starting with
a if(my_var < 3) { statement_one(); statement_two(); statement_three(); } The ';' statementAs stated The ConditionalsConditional statements are used a lot in LPC, and there are
several ways of writing them. A very important concept is that
The if and else statementsThe most common conditional
statement is the if (expression) statement; e.g. if (a == 5) a -= 4; If you want to handle the false case, you can add an
if (expression) true-statement else false-statement; e.g. if (a == 5) a -= 4; else a += 18; or if(a > 10) a -= 10; else { a += 100; b--; a -= 10; } The switch statementIf one variable has to be tested for a lot of different values, the resulting list of `if-else-if-else' statements soon gets very long and hard to read. However, if the type of the value you are testing is an integer, a float or a string you can use a much denser and neater way of coding. Assume you have the following code you want to write: if (name == "fatty") { nat = "se"; desc = "blimp"; } else if (name == "plugh") { nat = "no"; desc = "warlock"; } else if (name == "olorin") { nat = "de"; desc = "bloodshot"; } else { nat = "x"; desc = "unknown"; } A better way of writing this is: switch (name) { case "fatty": nat = "se"; desc = "blimp"; break; case "plugh": nat = "no"; desc = "warlock"; break; case "olorin": nat = "de"; desc = "bloodshot"; break; default: nat = "x"; desc = "unknown"; } The workings of this statement is simple: Note that the After a match has been found the following statements are
executed until a While it's not mandatory to have a If you forget to put in a 'break' statement the following 'case' expression will be executed. This might sound like something you don't want, but if in the example above the names `fatty' and `plugh' both should generate the same result you could write: case "fatty": /* FALLTHROUGH */ case "plugh": < code > break; ... and save a bit of space. Writing code with switch doesn't
make it any quicker to execute. It does make it a lot easier
to read, which reduces the chance of making mistakes while coding.
Remember to put the The ? : expressionThis is a very condensed way of
writing an Suppose you want to write the following: if (test_expression) var = if_expression; else var = else_expression; You can write that in a much more condensed way: var = test_expression ? if_expression : else_expression; e.g. name = day == 2 ? "tuesday" : "another day"; Opinions vary as to whether writing the conditional with this
operator makes the code easier or harder to read. A common rule of
thumb is that one use of the name = day == 2 ? time == 18 ? "miller time" : "tuesday" : "another day"; Loop statementsThere are two loop statements in LPC which incorporate the use of conditional statements within them. That means they can be programmed to execute only until a certain condition is true. The for statementIf you want a counter or an iterator, you should usually use the for statement. The syntax is as follows: for (initalize_statement ; test_expression ; end_of_loop_statement) body_statement;When first entered, the for statement executes
the initialize_statement part. This part usually is used to
initialize counters or values for the loop itself. Then the first
loop starts. Every loop starts by executing the
test_expression and examining the result. This is a truth
conditional, so any answer not equal to 0 or
nil will cause the loop to be run. If the test
expression is true then the body_statement is executed,
immediately followed by the end_of_loop_statement. In the
body_statement you usually do what you want to have done for
this iteration. In the end_of_loop_statement you usually
increment or decrement counters as needed to prepare them for the
test_expression in the next loop.
Throughout the previous section I used the word usually a lot. This is because you don't have to do it that way, there's no rule forcing you to make use of the statements in the way I said. For the moment let's stick to the regular way of using the for-statement. Later on I'll describe more refined techniques, and you can discover your own as well. Assume you want to compute the sum of all integers from 7 to 123 and don't know the formula ((x2^2 + x1^2) / 2). The most straightforward way of doing that is a loop. result = 0; for (count = 7 ; count < 124 ; count++) result += count; First of all, The loop form above is pretty standard in C, but you may have realized there's another way you can write the same thing: result = 0; for (count = 7 ; count <= 123 ; count++) result += count;This way works fine too, and you may find it more understandable. Then again, you may not. Please note that the value of count after the The while statementThe while statement is
pretty straightforward. You can probably guess from its name what
it does. The statement will perform another statement over and over
until a given while (<test expression>) Note carefully that the test expression is checked first of all, before running the statement the first time. If it evaluates as false the first time, the body is never executed. a = 0; while (a != 4) { a += 5; a /= 2; } The break and continue statementSometimes during the execution of while (end_condition < 9999) { /* If the time() function returns 29449494, abort execution */ if (time() == 29449494) break; < code > } /* Continue here both after a break or when the full loop is done. */ < code > Sometimes you merely want to start over from the top of the
/* Add all even numbers */ sum = 0; for (i = 0 ; i < 10000 ; i++) { /* Start from the top of the loop if 'i' is an odd number */ if (i % 2) continue; sum += i; }Notice that the i++ is executed when the loop is
continued. Only the sum += i; is skipped.
|