Simkin Switch Statement


This statement allows you to branch to different blocks of code depending on the value of an expression.

The statement has 3 main sections: an expression is given within a "switch" keyword. Then a series of "case" statements follow, each of which specifies a test expression followed by some statements to execute if the switch and case expression match.

Finally you can specify a "default" set of statements to execute.

The format is:

switch (<test-expression>) {
  case <compare-expression> {
    <statements...>
  }
  default {
    <statements...>
  }
}

For example:

i=0;
j=1;
switch(i){
  case j-1 {
  i=i+1;
 }
 default {
  j=j-1;
 }
}
will match the first case, and
i=i+1;
will be executed.

Note: you have to use the braces "{" and "}" to surround the statements after each case clause, and after the default clause.