📄 chapter7
字号:
7.5 for() loops
Prototype:
for(initialize values ; test expression ; instruction) { instructions }
initialize values:
This allows you to set starting values of variables which will be used
in the loop. This part is optional.
test expression:
Same as the expression in if() and while(). The loop is executed
as long as this expression (or expressions) is true. You must have a
test expression.
instruction:
An expression (or expressions) which is to be executed at the end of each
loop. This is optional.
Note:
for(;expression;) {}
IS EXACTLY THE SAME AS
while(expression) {}
Example:
1 int x;
2
3 for(x= (int)this_player()->query_level(); x>0; x--) {
4 if(random(2)) this_player()->add_money("silver", -random(50));
5 else this_player()->add_hp(-random(10));
6 }
This for() loop behaves EXACTLY like the while() example.
Additionally, if you wanted to initialize 2 variables:
for(x=0, y=random(20); x<y; x++) { write(x+"\n"); }
Here, we initialize 2 variables, x and y, and we separate them by a
comma. You can do the same with any of the 3 parts of the for()
expression.
7.6 The statement: switch()
Prototype:
switch(expression) {
case constant: instructions
case constant: instructions
...
case constant: instructions
default: instructions
}
This is functionally much like if() expressions, and much nicer to the
CPU, however most rarely used because it looks so damn complicated.
But it is not.
First off, the expression is not a test. The cases are tests. A English
sounding way to read:
1 int x;
2
3 x = random(5);
4 switch(x) {
5 case 1: write("X is 1.\n");
6 case 2: x++;
7 default: x--;
8 }
9 write(x+"\n");
is:
set variable x to a random number between 0 and 4.
In case 1 of variable x write its value add 1 to it and subtract 1.
In case 2 of variable x, add 1 to its value and then subtract 1.
In other cases subtract 1.
Write the value of x.
switch(x) basically tells the driver that the variable x is the value
we are trying to match to a case.
Once the driver finds a case which matches, that case *and all following
cases* will be acted upon. You may break out of the switch statement
as well as any other flow control statement with a break instruction in
order only to execute a single case. But that will be explained later.
The default statement is one that will be executed for any value of
x so long as the switch() flow has not been broken. You may use any
data type in a switch statement:
string name;
name = (string)this_player()->query_name();
switch(name) {
case "descartes": write("You borg.\n");
case "flamme":
case "forlock":
case "shadowwolf": write("You are a Nightmare head arch.\n");
default: write("You exist.\n");
}
For me, I would see:
You borg.
You are a Nightmare head arch.
You exist.
Flamme, Forlock, or Shadowwolf would see:
You are a Nightmare head arch.
You exist.
Everyone else would see:
You exist.
7.7 Altering the flow of functions and flow control statements
The following instructions:
return continue break
alter the natural flow of things as described above.
First of all,
return
no matter where it occurs in a function, will cease the execution of that
function and return control to the function which called the one the
return statement is in. If the function is NOT of type void, then a
value must follow the return statement, and that value must be of a
type matching the function. An absolute value function would look
like this:
int absolute_value(int x) {
if(x>-1) return x;
else return -x;
}
In the second line, the function ceases execution and returns to the calling
function because the desired value has been found if x is a positive
number.
continue is most often used in for() and while statements. It serves
to stop the execution of the current loop and send the execution back
to the beginning of the loop. For instance, say you wanted to avoid
division by 0:
x= 4;
while( x > -5) {
x--
if(!x) continue;
write((100/x)+"\n");
}
write("Done.\n")
You would see the following output:
33
50
100
-100
-50
-33
-25
Done.
To avoid an error, it checks in each loop to make sure x is not 0.
If x is zero, then it starts back with the test expression without
finishing its current loop.
In a for() expression
for(x=3; x>-5; x--) {
if(!x) continue;
write((100/x)+"\n");
}
write("Done.\n");
It works much the same way. Note this gives exactly the same output
as before. At x=1, it tests to see if x is zero, it is not, so it
writes 100/x, then goes back to the top, subtracts one from x, checks to
see if it is zero again, and it is zero, so it goes back to the top
and subtracts 1 again.
break
This one ceases the function of a flow control statement. No matter
where you are in the statement, the control of the program will go
to the end of the loop. So, if in the above examples, we had
used break instead of continue, the output would have looked like this:
33
50
100
Done.
continue is most often used with the for() and while() statements.
break however is mostly used with switch()
switch(name) {
case "descartes": write("You are borg.\n"); break;
case "flamme": write("You are flamme.\n"); break;
case "forlock": write("You are forlock.\n"); break;
case "shadowwolf": write("You are shadowwolf.\n"); break;
default: write("You will be assimilated.\n");
}
This functions just like:
if(name == "descartes") write("You are borg.\n");
else if(name == "flamme") write("You are flamme.\n");
else if(name == "forlock") write("You are forlock.\n");
else if(name == "shadowwolf") write("You are shadowwolf.\n");
else write("You will be assimilated.\n");
except the switch statement is much better on the CPU.
If any of these are placed in nested statements, then they alter the
flow of the most immediate statement.
7.8 Chapter summary
This chapter covered one hell of a lot, but it was stuff that needed to
be seen all at once. You should now completely understand if() for()
while() do{} while() and switch(), as well as how to alter their flow
using return, continue, and break. Effeciency says if it can be done in
a natural way using switch() instead of a lot of if() else if()'s, then
by all means do it. You were also introduced to the idea of calling
functions in other objects. That however, is a topic to be detailed later.
You now should be completely at ease writing simple rooms (if you have
read your mudlib's room building document), simple monsters, and
other sorts of simple objects.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -