Switch: Difference between revisions

From EDukeWiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 13: Line 13:
   endswitch
   endswitch


The '''case''' keyword works like an [[ifvare]] statement, and '''default''' operates on every possibility that is ''not'' specified by a case statement.
The '''case''' keyword works like an [[ifvare]] statement, and '''default''' operates on every possibility that is ''not'' specified by a case statement. Each case block must be terminated with a '''[[break]]''' statement.


Caution:  Nested switches do not work because of the way the CON compiler compiles them. A workaround is to put the nested switch in a separate state, and call that state from inside the first switch.
Caution:  Nested switches do not work because of the way the CON compiler compiles them. A workaround is to put the nested switch in a separate state, and call that state from inside the first switch.

Revision as of 12:23, 20 February 2012

The switch statement in CON is similar to that in C.

 switch <gamevar>
   case <constant>
     <do something>
   break
   case <constant>
     <do something>
   break
   default
     <do something>
   break
 endswitch

The case keyword works like an ifvare statement, and default operates on every possibility that is not specified by a case statement. Each case block must be terminated with a break statement.

Caution: Nested switches do not work because of the way the CON compiler compiles them. A workaround is to put the nested switch in a separate state, and call that state from inside the first switch.

Also, adding extra break statements inside a case argument will cause a lot of errors. Again, the workaround is to put the commands that need to be separated in another state, along with the break.

See: Control Structures @ C++ Documentation