📄 chapter7
字号:
LPC Basics
Written by Descartes of Borg
first edition: 23 april 1993
second edition: 10 july 1993
CHAPTER 7: Flow Control
7.1 Review of variables
Variables may be manipulated by assigning or changing values with the
expressions =, +=, -=, ++, --. Those expressions may be combined with
the expressions -, +, *, /, %. However, so far, you have only been
shown how to use a function to do these in a linear way. For example:
int hello(int x) {
x--;
write("Hello, x is "+x+".\n");
return x;
}
is a function you should know how to write and understand. But what
if you wanted to write the value of x only if x = 1? Or what if
you wanted it to keep writing x over and over until x = 1 before
returning? LPC uses flow control in exactly the same way as C and C++.
7.2 The LPC flow control statements
LPC uses the following expressions:
if(expression) instruction;
if(expression) instruction;
else instruction;
if(expression) instruction;
else if(expression) instruction;
else instruction;
while(expression) instruction;
do { instruction; } while(expression);
switch(expression) {
case (expression): instruction; break;
default: instruction;
}
Before we discuss these, first something on what is meant by expression and
instruction. An expression is anything with a value like a variable,
a comparison (like x>5, where if x is 6 or more, the value is 1, else the
value is 0), or an assignment(like x += 2). An instruction can be any
single line of lpc code like a function call, a value assignment or
modification, etc.
You should know also the operators &&, ||, ==, !=, and !. These are the
logical operators. They return a nonzero value when true, and 0 when false.
Make note of the values of the following expressions:
(1 && 1) value: 1 (1 and 1)
(1 && 0) value: 0 (1 and 0)
(1 || 0) value: 1 (1 or 0)
(1 == 1) value: 1 (1 is equal to 1)
(1 != 1) value: 0 (1 is not equal to 1)
(!1) value: 0 (not 1)
(!0) value: 1 (not 0)
In expressions using &&, if the value of the first item being compared
is 0, the second is never tested even. When using ||, if the first is
true (1), then the second is not tested.
7.3 if()
The first expression to look at that alters flow control is if(). Take
a look at the following example:
1 void reset() {
2 int x;
3
4 ::reset();
5 x = random(10);
6 if(x > 50) set_search_func("floorboards", "search_floor");
7 }
The line numbers are for reference only.
In line 2, of course we declare a variable of type int called x. Line 3
is aethetic whitespace to clearly show where the declarations end and the
function code begins. The variable x is only available to the function
reset().
Line 4 makes a call to the room.c version of reset().
Line 5 uses the driver efun random() to return a random number between
0 and the parameter minus 1. So here we are looking for a number between
0 and 99.
In line 6, we test the value of the expression (x>50) to see if it is true
or false. If it is true, then it makes a call to the room.c function
set_search_func(). If it is false, the call to set_search_func() is never
executed.
In line 7, the function returns driver control to the calling function
(the driver itself in this case) without returning any value.
If you had wanted to execute multiple instructions instead of just the one,
you would have done it in the following manner:
if(x>50) {
set_search_func("floorboards", "search_floor");
if(!present("beggar", this_object())) make_beggar();
}
Notice the {} encapsulate the instructions to be executed if the test
expression is true. In the example, again we call the room.c function
which sets a function (search_floor()) that you will later define yourself
to be called when the player types "search floorboards" (NOTE: This is
highly mudlib dependent. Nightmare mudlibs have this function call.
Others may have something similar, while others may not have this feature
under any name). Next, there is another if() expression that tests the
truth of the expression (!present("beggar",this_object())). The ! in the
test expression changes the truth of the expression which follows it. In
this case, it changes the truth of the efun present(), which will return
the object that is a beggar if it is in the room (this_object()), or it
will return 0 if there is no beggar in the room. So if there is a beggar
still living in the room, (present("beggar", this_object())) will have
a value equal to the beggar object (data type object), otherwise it will
be 0. The ! will change a 0 to a 1, or any nonzero value (like the
beggar object) to a 0. Therefore, the expression
(!present("beggar", this_object())) is true if there is no beggar in the
room, and false if there is. So, if there is no beggar in the room,
then it calls the function you define in your room code that makes a
new beggar and puts it in the room. (If there is a beggar in the room,
we do not want to add yet another one :))
Of course, if()'s often comes with ands or buts :). In LPC, the formal
reading of the if() statement is:
if(expression) { set of intructions }
else if(expression) { set of instructions }
else { set of instructions }
This means:
If expression is true, then do these instructions.
Otherise, if this second expression is true, do this second set.
And if none of those were true, then do this last set.
You can have if() alone:
if(x>5) write("Foo,\n");
with an else if():
if(x > 5) write("X is greater than 5.\n");
else if(x >2) write("X is less than 6, but greater than 2.\n");
with an else:
if(x>5) write("X is greater than 5.\n");
else write("X is less than 6.\n");
or the whole lot of them as listed above. You can have any number of
else if()'s in the expression, but you must have one and only one
if() and at most one else. Of course, as with the beggar example,
you may nest if() statements inside if() instructions. (For example,
if(x>5) {
if(x==7) write("Lucky number!\n");
else write("Roll again.\n");
}
else write("You lose.\n");
7.4 The statements: while() and do {} while()
Prototype:
while(expression) { set of instructions }
do { set of instructions } while(expression);
These allow you to create a set of instructions which continue to
execute so long as some expression is true. Suppose you wanted to
set a variable equal to a player's level and keep subtracting random
amounts of either money or hp from a player until that variable equals
0 (so that player's of higher levels would lose more). You might do it
this way:
1 int x;
2
3 x = (int)this_player()->query_level(); /* this has yet to be explained */
4 while(x > 0) {
5 if(random(2)) this_player()->add_money("silver", -random(50));
6 else this_player()->add_hp(-(random(10));
7 x--;
8 }
The expression this_player()->query_level() calIn line 4, we start a loop that executes so long as x is greater than 0.
Another way we could have done this line would be:
while(x) {
The problem with that would be if we later made a change to the funtion
y anywhere between 0 and 49 coins.
In line 6, if instead it returns 0, we call the add_hp() function in the
player which reduces the player's hit points anywhere between 0 and 9 hp.
In line 7, we reduce x by 1.
At line 8, the execution comes to the end of the while() instructions and
goes back up to line 4 to see if x is still greater than 0. This
loop will keep executing until x is finally less than 1.
You might, however, want to test an expression *after* you execute some
instructions. For instance, in the above, if you wanted to execute
the instructions at least once for everyone, even if their level is
below the test level:
int x;
x = (int)this_player()->query_level();
do {
if(random(2)) this_player()->add_money("silver", -random(50));
else this_player()->add_hp(-random(10));
x--;
} while(x > 0);
This is a rather bizarre example, being as few muds have level 0 players.
And even still, you could have done it using the original loop with
a different test. Nevertheless, it is intended to show how a do{} while()
works. As you see, instead of initiating the test at the beginning of the
loop (which would immediately exclude some values of x), it tests after
the loop has been executed. This assures that the instructions of the loop
get executed at least one time, no matter what x is.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -