Control:Switch

From Lavish Software Wiki
Jump to navigation Jump to search

Forms

Switch

switch <text>
{
  <cases and commands>
}

Switches are used to selectively execute a single block of code out of several choices, called cases. For example, it may execute a different block of code depending on the name of a fruit, such as apple, orange, banana or watermelon. If no cases are found that match the given text, the default case is used if it exists. If no cases are found that match, and default does not exist, the entire switch is skipped.

Case

case <text>

Cases define the start of a block of code, labelled by the given text. Cases are literal text, are not checked for data sequences, and do not have quotes stripped (i.e. any quotes or other text in your case must match the literal text resulting from the switch, and spaces may be used freely). Cases are not case sensitive, so Apple is the same is apple is the same as appLE. A case does not imply the end of the previous case. See [[Control:Switch#Break|]].

VariableCase

variablecase <text>

Variable cases are similar to cases, but are checked for data sequences. If no normal case matches, each variable case will be processed and checked for a match, from top to bottom. If no variable case exists or matches, the flow continues to the "default" if it exists, otherwise skipping past the switch.

Default

default

Default is the start of the "default" block of code, which is executed if no cases that match the switch text exist. This is not required, as if no cases are found that match and no default exists, the switch will simply be skipped entirely. Default does not imply the end of the previous case. See [[Control:Switch#Break|]].

Break

break

Break is used to immediately skip the rest of the switch. Without breaking, the flow simply continues until the switch ends.

Examples

Example 1

Switch ${Fruit}
{
  case Apple
  case Banana
    echo Apple or Banana
    break
  case Cherry
    echo Cherry
    break
  case Grape
  case Kiwi
  case Orange
    echo Grape, Kiwi or Orange
    break
  case Pineapple
  case Watermelon
    echo Pineapple or Watermelon
    break
  variablecase ${SomeOtherFruit}
    echo Matched some other fruit
    break
  default
    echo Unknown fruit ${Fruit}
}

See Also