<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://wiki.lostsouls.org/w/skins/common/feed.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Man switch - Revision history</title>
		<link>http://wiki.lostsouls.org/w/index.php?title=Man_switch&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.8.2</generator>
		<lastBuildDate>Thu, 07 May 2026 02:56:03 GMT</lastBuildDate>
		<item>
			<title>Laine at 16:59, 11 June 2007</title>
			<link>http://wiki.lostsouls.org/w/index.php?title=Man_switch&amp;diff=3933&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
==Synopsis==&lt;br /&gt;
&lt;br /&gt;
switch (expr) block;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Branch to the case label in statement that matches expr.&lt;br /&gt;
If no matching case label is found (by value or by type),&lt;br /&gt;
branch to the default label in statement.&lt;br /&gt;
&lt;br /&gt;
A case label has the form&lt;br /&gt;
&lt;br /&gt;
                case expr_n :&lt;br /&gt;
&lt;br /&gt;
where expr_n must be constant, or the form&lt;br /&gt;
&lt;br /&gt;
                case expr_n1 .. expr_n2 :&lt;br /&gt;
&lt;br /&gt;
where expr_n1 and expr_n2 must be numeric constants and&lt;br /&gt;
expr_n1 &amp;lt; expr_n2.&lt;br /&gt;
&lt;br /&gt;
Either all case labels have to be strings or all have to be&lt;br /&gt;
numeric. Only 0 is special: it is allowed in a switch&lt;br /&gt;
statement where all other labels are strings.&lt;br /&gt;
&lt;br /&gt;
A default label has the form&lt;br /&gt;
&lt;br /&gt;
                default :&lt;br /&gt;
&lt;br /&gt;
The default label defaults to the end of statement if not&lt;br /&gt;
given explicitly.&lt;br /&gt;
&lt;br /&gt;
Whenever a 'break' statement is executed inside 'statement' a&lt;br /&gt;
branch to the end of the switch statement is performed.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Typical usage:&lt;br /&gt;
&lt;br /&gt;
        switch(random(100)) {&lt;br /&gt;
          case 0 .. 22 : write(&amp;quot;Nothing happens&amp;quot;); break;&lt;br /&gt;
          case 23 .. 27 :&lt;br /&gt;
            write(&amp;quot;You are surrounded by a golden glow&amp;quot;);&lt;br /&gt;
            this_player()-&amp;gt;heal_self(random(3));&lt;br /&gt;
            break;&lt;br /&gt;
          case 28 .. 32 :&lt;br /&gt;
            write(&amp;quot;The water was poisoned!\n&amp;quot;);&lt;br /&gt;
            this_player()-&amp;gt;add_exp(this_player()-&amp;gt;hit_player(random(4)));&lt;br /&gt;
            break;&lt;br /&gt;
          case 33 : write(&amp;quot;You hear a voice whispering: &amp;quot;+random_hint());&lt;br /&gt;
          /* fall through */&lt;br /&gt;
          case 34 :&lt;br /&gt;
            write(&amp;quot;While you didn't paid attention, a water demon snatches\n&amp;quot;&lt;br /&gt;
                  &amp;quot;a coin out of your purse!\n&amp;quot;);&lt;br /&gt;
            this_player()-&amp;gt;add_money(-1);&lt;br /&gt;
            break;&lt;br /&gt;
          default : write &amp;quot;You hear some strange noises\n&amp;quot;; break;&lt;br /&gt;
          case 42 : return;&lt;br /&gt;
          case 99 : write(&amp;quot;It tastes good.\n&amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
==Bugs==&lt;br /&gt;
&lt;br /&gt;
In C, the grammar for switch() is&lt;br /&gt;
&lt;br /&gt;
            switch (expr) block&lt;br /&gt;
&lt;br /&gt;
allowing constructs like&lt;br /&gt;
&lt;br /&gt;
            switch (expr)&lt;br /&gt;
              while (expr2)&lt;br /&gt;
              {&lt;br /&gt;
              case 1: ...&lt;br /&gt;
              case 2: ...&lt;br /&gt;
              }&lt;br /&gt;
&lt;br /&gt;
The LPC compiler currently can't handle neither statements as switch&lt;br /&gt;
body, nor case labels embedded in inner loops.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
LDMud 3.2.9/3.3 allowed to pass values of the wrong type to switch(),&lt;br /&gt;
the driver would in that case use the default case. Before, values&lt;br /&gt;
of the wrong type caused a runtime error.&lt;br /&gt;
&lt;br /&gt;
LDMud 3.2.10 constrained the grammar to require a block for the&lt;br /&gt;
switch() body, not just a statement. This differs from the C&lt;br /&gt;
syntax, but was necessary as the compiler didn't handle&lt;br /&gt;
the statement case correctly.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[[man for|for(LPC)]], [[man foreach|foreach(LPC)]], [[man do while|do-while(LPC)]], [[man if|if(LPC)]], [[man while|while(LPC)]]&lt;/div&gt;</description>
			<pubDate>Mon, 11 Jun 2007 16:59:01 GMT</pubDate>			<dc:creator>Laine</dc:creator>			<comments>http://wiki.lostsouls.org/Talk:Man_switch</comments>		</item>
	</channel>
</rss>