blob: 9bca5963baaff2dc35a1f6b7c1c5e6ec5de06d78 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001NAME
2 switch
3
4SYNTAX
5 switch (expr) block
6
7DESCRIPTION
8 Branch to the case label in statement that matches expr.
9 If no matching case label is found (by value or by type),
10 branch to the default label in statement.
11
12 A case label has the form
13
14 case expr_n :
15
16 where expr_n must be constant, or the form
17
18 case expr_n1 .. expr_n2 :
19
20 where expr_n1 and expr_n2 must be numeric constants and
21 expr_n1 < expr_n2.
22
23 Either all case labels have to be strings or all have to be
24 numeric. Only 0 is special: it is allowed in a switch
25 statement where all other labels are strings.
26
27 A default label has the form
28
29 default :
30
31 The default label defaults to the end of statement if not
32 given explicitly.
33
34 Whenever a 'break' statement is executed inside 'statement' a
35 branch to the end of the switch statement is performed.
36
37EXAMPLE
38 Typical usage:
39
40 switch(random(100)) {
41 case 0 .. 22 : write("Nothing happens"); break;
42 case 23 .. 27 :
43 write("You are surrounded by a golden glow");
44 this_player()->heal_self(random(3));
45 break;
46 case 28 .. 32 :
47 write("The water was poisoned!\n");
48 this_player()->add_exp(this_player()->hit_player(random(4)));
49 break;
50 case 33 : write("You hear a voice whispering: "+random_hint());
51 /* fall through */
52 case 34 :
53 write("While you didn't pay attention, a water demon "
54 "snatches a coin out of your purse!\n");
55 this_player()->add_money(-1);
56 break;
57 default : write "You hear some strange noises\n"; break;
58 case 42 : return;
59 case 99 : write("It tastes good.\n";
60 }
61
62NOTE
63 In C, the grammar for switch() is
64
65 switch (expr) statement
66
67 allowing constructs like
68
69 switch (expr)
70 while (expr2)
71 {
72 case 1: ...
73 case 2: ...
74 }
75
76 In LPC a switch has to be followed by a block that contains the
77 case labels directly. In contrast to C the group of statements
78 following a case label have their own lexical scope so that
79 variable declarations may not cross case labels.
80
81HISTORY
82 LDMud 3.2.10 constrained the grammar to require a block for the
83 switch() body, not just a statement. This differs from the C
84 syntax, but was necessary as the compiler didn't handle
85 the statement case correctly.
86 LDMud 3.3 allowed to pass values of the wrong type to switch(), the
87 driver would in that case use the default case. Before, values of
88 the wrong type caused a runtime error.
89 LDMud 3.3.718 disallowed case labels in inner blocks and variable
90 declarations that cross case labels.
91
92SEE ALSO
93 for(LPC), foreach(LPC), do-while(LPC), if(LPC), while(LPC)